最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create a Date Object with the year only - Stack Overflow

programmeradmin0浏览0评论

I'm used to create Date objects by using the fourth syntax from MDN as new Date(year, month, day, hours, minutes, seconds, milliseconds); But lately I tried to set a Date object with only a year (as new Date(2017)) but as you could expect it was treated as a value and considered the year as a number of milliseconds.

Is there any way of still easily use the year as is without changing the syntax and expect a correctly set Date ?

I'm used to create Date objects by using the fourth syntax from MDN as new Date(year, month, day, hours, minutes, seconds, milliseconds); But lately I tried to set a Date object with only a year (as new Date(2017)) but as you could expect it was treated as a value and considered the year as a number of milliseconds.

Is there any way of still easily use the year as is without changing the syntax and expect a correctly set Date ?

Share Improve this question asked Dec 27, 2017 at 14:18 PaulCoPaulCo 1,4875 gold badges18 silver badges34 bronze badges 1
  • 3 what date would you expect to be set when giving only a year ? And if you do have an expectation, then why not provide the relevant parameters ? – Gabriele Petrioli Commented Dec 27, 2017 at 14:21
Add a ment  | 

4 Answers 4

Reset to default 8

Two solutions e to my mind:

(1) Set the year argument to 2017 and set the month argument to 0 when constructing the date:

let d = new Date(2017, 0);
console.log(d.toString());

The arguments will be treated as local time; month and day of month will be January 1; all time ponents will be set to 0.

(2) Specify "2017T00:00" as the first and only argument when constructing the date:

let d = new Date("2017T00:00");
console.log(d.toString());

According to current specs this is a valid format and browsers are supposed to treat it as local time. The behavior is same as that of previous example.

If you are passing a single parameter (number or string), then it is taken as per doc

value

Integer value representing the number of milliseconds since January 1, 1970 00:00:00 UTC, with leap seconds ignored (Unix Epoch; but consider that most Unix time stamp functions count in seconds).

dateString

String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-pliant RFC 2822 timestamps and also a version of ISO8601).

Also as per doc

If at least two arguments are supplied, missing arguments are either set to 1 (if day is missing) or 0 for all others.

You can pass one more parameter as 0 or null (or the actual value you want to set)

new Date(2017,0);

Demo

var date = new Date(2017,0);
console.log( date );

You could pass null as second argument:

new Date(2017, null);

However, without knowing the details of how missing values are interpreted, what do you think happens now? Will the date be initialized with the current month, day, hour, etc? Or something different?

Better be explicit and pass all arguments, so that you know what the code is doing half a year later.

I have another suggestion. You can just create a regular date object and set it's year. So at least you know to expect what the rest of the Date object values are.

var year = "2014";

var date = new Date();
date.setFullYear(year);

// console.log(year) => Wed Dec 27 2014 16:25:28 GMT+0200

Further reading - Date.prototype.setFullYear()

发布评论

评论列表(0)

  1. 暂无评论