mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 21:50:00 -05:00
feat(std/datetime): Added weekOfYear (#6659)
This commit is contained in:
parent
edb7a0eead
commit
634d6af7a1
3 changed files with 47 additions and 4 deletions
|
@ -37,3 +37,13 @@ import {
|
|||
dayOfYear(new Date("2019-03-11T03:24:00")); // output: 70
|
||||
currentDayOfYear(); // output: ** depends on when you run it :) **
|
||||
```
|
||||
|
||||
### weekOfYear
|
||||
|
||||
- `weekOfYear(date: Date)` - Returns the week number of the provided date (1-53)
|
||||
|
||||
```ts
|
||||
import { weekOfYear } from "https://deno.land/std/datetime/mod.ts";
|
||||
|
||||
weekOfYear(new Date("2020-12-28T03:24:00")); // Returns 53
|
||||
```
|
||||
|
|
|
@ -110,6 +110,30 @@ export function dayOfYear(date: Date): number {
|
|||
return Math.floor(diff / DAY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of the week in the year (ISO-8601)
|
||||
* @return Number of the week in year
|
||||
*/
|
||||
export function weekOfYear(date: Date): number {
|
||||
const workingDate = new Date(
|
||||
Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())
|
||||
);
|
||||
|
||||
// Set to nearest Thursday: current date + 4 - current day number
|
||||
// Make Sunday's day number 7
|
||||
workingDate.setUTCDate(
|
||||
workingDate.getUTCDate() + 4 - (workingDate.getUTCDay() || 7)
|
||||
);
|
||||
|
||||
// Get first day of year
|
||||
const yearStart = new Date(Date.UTC(workingDate.getUTCFullYear(), 0, 1));
|
||||
|
||||
// return the calculated full weeks to nearest Thursday
|
||||
return Math.ceil(
|
||||
((workingDate.valueOf() - yearStart.valueOf()) / 86400000 + 1) / 7
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of current day in year
|
||||
* @return Number of current day in year
|
||||
|
|
|
@ -81,16 +81,25 @@ Deno.test({
|
|||
Deno.test({
|
||||
name: "[std/datetime] DayOfYear",
|
||||
fn: () => {
|
||||
assertEquals(1, datetime.dayOfYear(new Date("2019-01-01T03:24:00")));
|
||||
assertEquals(70, datetime.dayOfYear(new Date("2019-03-11T03:24:00")));
|
||||
assertEquals(365, datetime.dayOfYear(new Date("2019-12-31T03:24:00")));
|
||||
assertEquals(datetime.dayOfYear(new Date("2019-01-01T03:24:00")), 1);
|
||||
assertEquals(datetime.dayOfYear(new Date("2019-03-11T03:24:00")), 70);
|
||||
assertEquals(datetime.dayOfYear(new Date("2019-12-31T03:24:00")), 365);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[std/datetime] currentDayOfYear",
|
||||
fn: () => {
|
||||
assertEquals(datetime.currentDayOfYear(), datetime.dayOfYear(new Date()));
|
||||
assertEquals(datetime.dayOfYear(new Date()), datetime.currentDayOfYear());
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[std/datetime] WeekOfYear",
|
||||
fn: () => {
|
||||
assertEquals(datetime.weekOfYear(new Date("2020-01-05T03:24:00")), 1);
|
||||
assertEquals(datetime.weekOfYear(new Date("2020-12-28T03:24:00")), 53); // 53 weeks in 2020
|
||||
assertEquals(datetime.weekOfYear(new Date("2020-06-28T03:24:00")), 26);
|
||||
},
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue