Comparing version 0.0.1 to 0.0.3
@@ -12,3 +12,3 @@ 'use strict'; | ||
function getWeekOfYear(d, mode) { | ||
function getWeekOfYear(d, mode = 0) { | ||
let ret = getYearWeek(d, mode); | ||
@@ -15,0 +15,0 @@ return ret.week; |
@@ -10,2 +10,3 @@ | ||
/* in-package */ | ||
, getWeek1Offset = require('./lib/getWeek1Offset') | ||
, getDayOfYear = require('./getDayOfYear') | ||
@@ -34,45 +35,4 @@ ; | ||
function getYearWeek(d, mode = 0) { | ||
// Get the weekday of Jan 1st. | ||
let Jan1st = new Date(d); | ||
Jan1st.setMonth(0); | ||
Jan1st.setDate(1); | ||
let weekdayOfJan1st = Jan1st.getDay(); | ||
// The offset from Jan 1st to the first day of week 1. | ||
// 0 means Jan 1st is just the first day of week 1. | ||
// Negative offset means the first day of week 1 is actually in last year. | ||
let week1Offset = null; | ||
// First day of week: Sunday | ||
// Week 1 is the first week with a Sunday in this year. Namely, | ||
// Week 1 is the first week with a Thursday in this year. | ||
if (mode == 0 || mode == 2) { | ||
week1Offset = (7 - weekdayOfJan1st) % 7; | ||
} | ||
// First day of week: Monday | ||
// Week 1 is the first week with a Monday in this year. | ||
else if (mode == 5 || mode == 7) { | ||
week1Offset = (8 - weekdayOfJan1st) % 7; | ||
} | ||
// First day of week: Sunday | ||
// Week 1 is the first week with 4 or more days this year. | ||
else if (mode == 4 || mode == 6) { | ||
week1Offset = [0,1,2,3].includes(weekdayOfJan1st) | ||
? 0 - weekdayOfJan1st | ||
: 7 - weekdayOfJan1st | ||
; | ||
} | ||
// First day of week: Monday | ||
// Week 1 is the first week with 4 or more days this year. | ||
else if (mode == 1 || mode == 3) { | ||
week1Offset = [1,2,3,4].includes(weekdayOfJan1st) | ||
? 1 - weekdayOfJan1st | ||
: (8 - weekdayOfJan1st) % 7 | ||
; | ||
} | ||
let year = d.getFullYear(); | ||
let week1Offset = getWeek1Offset(year, mode); | ||
let daysOfYear = getDayOfYear(d); | ||
@@ -82,4 +42,3 @@ | ||
if (daysOfYear <= week1Offset && [2,3,6,7].includes(mode)) { | ||
let Dec31st = new Date(Jan1st); | ||
Dec31st.setDate(0); | ||
let Dec31st = new Date(year - 1, 11, 31); | ||
return getYearWeek(Dec31st, mode); | ||
@@ -86,0 +45,0 @@ } |
@@ -15,4 +15,5 @@ 'use strict'; | ||
getWeekOfYear : require('./getWeekOfYear'), | ||
getWeekRange : require('./getWeekRange'), | ||
getYearWeek : require('./getYearWeek'), | ||
isLeapYear : require('./isLeapYear'), | ||
}; |
@@ -14,3 +14,3 @@ { | ||
"name": "dafo", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"main": "index.js", | ||
@@ -17,0 +17,0 @@ "keywords": [ |
# dafo | ||
__General Date Format__ | ||
[![total downloads of dafo](https://img.shields.io/npm/dt/dafo.svg)](https://www.npmjs.com/package/dafo) | ||
[![dafo's License](https://img.shields.io/npm/l/dafo.svg)](https://www.npmjs.com/package/dafo) | ||
[![latest version of dafo](https://img.shields.io/npm/v/dafo.svg)](https://www.npmjs.com/package/dafo) | ||
[![build status of github.com/YounGoat/ecmascript.dafo](https://travis-ci.org/YounGoat/ecmascript.dafo.svg?branch=master)](https://travis-ci.org/YounGoat/ecmascript.dafo) | ||
## Table of contents | ||
@@ -180,5 +185,10 @@ | ||
* { Date *first*, Date *last* } __dafo.getWeekRange__({ number *year*, number *week*, number *mode* }) | ||
Get the week range. | ||
Read section [Week Mode](#week-mode) for details about the *mode* argument. | ||
* { *year*, *week* } __dafo.getYearWeek__(Date *date*, number *mode*) | ||
Get the week number of year, and the corresponding year. | ||
In some *mode*, the first few days in a year may be belonging to the last week of last year. Read section [Week Mode](#week-mode) for details about the *mode* argument. | ||
In some *mode*, the first few days in a year may be belonging to the last week of last year. | ||
Read section [Week Mode](#week-mode) for details about the *mode* argument. | ||
@@ -185,0 +195,0 @@ * boolean __dafo.isLeapYear__(Date *date*) |
@@ -195,5 +195,5 @@ 'use strict'; | ||
it('U, Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)', () => { | ||
assert.strictEqual(date_format(d1, 'U'), '946703646'); | ||
it.skip('U, Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)', () => { | ||
// TODO | ||
}); | ||
}); |
@@ -96,2 +96,8 @@ 'use strict'; | ||
}); | ||
it('getWeekRange()', () => { | ||
let range = dafo.getWeekRange({ year: 2018, week: 1, mode: 0 }); | ||
assert.equal(range.first.getDate(), 7); | ||
assert.equal(range.last.getDate(), 13); | ||
}); | ||
@@ -98,0 +104,0 @@ it('getYearWeek()', () => { |
Sorry, the diff of this file is not supported yet
49997
23
993
224