2024-01-01 21:11:32 +00:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2023-03-18 12:36:00 +00:00
// This module is browser compatible.
2022-12-19 10:24:48 +00:00
import { DAY } from "./constants.ts" ;
/ * *
2023-06-14 12:26:29 +00:00
* Returns the number of the day in the year in the local time zone .
2022-12-19 10:24:48 +00:00
*
2024-03-26 10:53:58 +00:00
* @param date Date to get the day of the year of .
* @return Number of the day in the year in the local time zone .
*
2024-04-11 11:31:27 +00:00
* @example Basic usage
2022-12-19 10:24:48 +00:00
* ` ` ` ts
2024-04-29 02:57:30 +00:00
* import { dayOfYear } from "@std/datetime/day-of-year" ;
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
* import { assertEquals } from "@std/assert" ;
2022-12-19 10:24:48 +00:00
*
2024-06-03 04:10:27 +00:00
* assertEquals ( dayOfYear ( new Date ( "2019-03-11T03:24:00" ) ) , 70 ) ;
2022-12-19 10:24:48 +00:00
* ` ` `
* /
export function dayOfYear ( date : Date ) : number {
// Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date)
// Using setFullYear as a workaround
const yearStart = new Date ( date ) ;
2023-06-14 12:26:29 +00:00
yearStart . setFullYear ( date . getFullYear ( ) , 0 , 0 ) ;
2023-09-26 05:04:05 +00:00
const diff = ( date . getTime ( ) - date . getTimezoneOffset ( ) * 60 * 1000 ) -
( yearStart . getTime ( ) - yearStart . getTimezoneOffset ( ) * 60 * 1000 ) ;
2023-06-14 12:26:29 +00:00
return Math . floor ( diff / DAY ) ;
}
/ * *
* Returns the number of the day in the year in UTC time .
*
2024-03-26 10:53:58 +00:00
* @param date Date to get the day of the year of .
* @return Number of the day in the year in UTC time .
*
2024-05-22 05:08:36 +00:00
* @example Usage
2023-06-14 12:26:29 +00:00
* ` ` ` ts
2024-04-29 02:57:30 +00:00
* import { dayOfYearUtc } from "@std/datetime/day-of-year" ;
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
* import { assertEquals } from "@std/assert" ;
2023-06-14 12:26:29 +00:00
*
2024-06-03 04:10:27 +00:00
* assertEquals ( dayOfYearUtc ( new Date ( "2019-03-11T03:24:00.000Z" ) ) , 70 ) ;
2023-06-14 12:26:29 +00:00
* ` ` `
* /
export function dayOfYearUtc ( date : Date ) : number {
// Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date)
// Using setUTCFullYear as a workaround
const yearStart = new Date ( date ) ;
2022-12-19 10:24:48 +00:00
yearStart . setUTCFullYear ( date . getUTCFullYear ( ) , 0 , 0 ) ;
2023-08-25 09:04:43 +00:00
const diff = date . getTime ( ) - yearStart . getTime ( ) ;
2022-12-19 10:24:48 +00:00
return Math . floor ( diff / DAY ) ;
}