2

I want to parse '2015' string to Date

Problem is that adjusts one hour because of winter time and timezone. Is it possible to get:

Fri Jan 01 2010 00:00:00 when I do new Date("2010") or moment("2010")

in case of:

Fri Jan 01 2010 01:00:00 GMT+0100 (W. Europe Standard Time)

I want always hours to be 00:00.

EDIT

Not mentioned: in my case I do not know in which format I get string of date. It can be year, year month, year month day hours etc. What I am asking is how to format string to date, that all unknown values would be: 01 for days and months, 00 for hours and minutes.

Edgar
  • 1,120
  • 4
  • 28
  • 53
  • 2
    Possible duplicate of [What is the best way to initialize a JavaScript Date to midnight?](http://stackoverflow.com/questions/3894048/what-is-the-best-way-to-initialize-a-javascript-date-to-midnight) –  Jan 19 '16 at 15:33

3 Answers3

2

You can set this information in Date constructor like this:

new Date(2010, 0); // params: year, month

Or if you need to create date from string:

var year = "2010";
new Date(parseInt(year), 0);
new Date(year + "-01-01");
madox2
  • 49,493
  • 17
  • 99
  • 99
  • not sure what it does exactly `new Date(year + "/1");` but it works. Thank you – Edgar Jan 19 '16 at 20:24
  • @Edgar—it "works" purely by chance. It creates a string like "2010/1", which is not a [*format supported by ECMA-262*](http://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format) so implementations may fall back to any parsing algorithm they like, including treating it as an invalid string and returning an invalid date. – RobG Jan 19 '16 at 22:59
  • @madox2 problem with new changes is that it will support `"2010 02"` string.date . Main problem is that I not sure in what format I will receive string. – Edgar Jan 20 '16 at 06:36
1

Using moment you can have:

moment("2015", "YYYY");
// 2015-01-01T00:00:00+01:00

if you need to work with your local time, while if you want to use utc, you can have:

moment.utc("2015", "YYYY");
// 2015-01-01T00:00:00+00:00

EDIT after your comment:

You can use moment parsing using an array of formats:

var s = "2015";
moment(s, ["YYYY", "YYYY MMMM"]);     // 2015-01-01T00:00:00+01:00
moment.utc(s, ["YYYY", "YYYY MMMM"]); // 2015-01-01T00:00:00+00:00
s = "2010 February";
moment(s, ["YYYY", "YYYY MMMM"]);     // 2010-02-01T00:00:00+01:00
moment.utc(s, ["YYYY", "YYYY MMMM"]); // 2010-02-01T00:00:00+00:00

EDIT #2 With moment you can use:

moment(s, ["YYYY MM DD HH:mm:ss", "YYYY MMMM DD HH:mm:ss"]);

For example:

function testMoment(s){
  var d = moment(s, ["YYYY MM DD HH:mm:ss", "YYYY MMMM DD HH:mm:ss"]);
  console.log( d.format() );
}

testMoment("2010");             // 2010-01-01T00:00:00+01:00
testMoment("2010 02");          // 2010-02-01T00:00:00+01:00
testMoment("2010 Feb");         // 2010-02-01T00:00:00+01:00
testMoment("2010 February");    // 2010-02-01T00:00:00+01:00
testMoment("2010 02 03");       // 2010-02-02T00:00:00+01:00
testMoment("2010 Feb 03");      // 2010-02-03T00:00:00+01:00
testMoment("2010 Feb 3");       // 2010-02-03T00:00:00+01:00
testMoment("2010 February 03"); // 2010-02-03T00:00:00+01:00
testMoment("2010 February 3");  // 2010-02-03T00:00:00+01:00
testMoment("2010 02 03 04");    // 2010-02-03T04:00:00+01:00
// etc...
VincenzoC
  • 30,117
  • 12
  • 90
  • 112
  • problem is that I do not know exact format, what I get, for example I can get, `"2010 February"` – Edgar Jan 19 '16 at 20:18
  • @Edgar I have updated my answer in order to manage multiple formats. – VincenzoC Jan 20 '16 at 08:30
  • I use it in quite generic component, once it can receive string like `"2004"` then `"2001 01 02"` etc. I get only string with date, without format type. – Edgar Jan 20 '16 at 09:22
  • Do you have a finite set of possible input formats? I understand that you can receive different format, but this can cause you issue: how `2001 01 02` should be interpreted? It could be both `1st February 2001` and `2nd January 2001` :( – VincenzoC Jan 20 '16 at 09:52
  • it always comes like: `year month day hour minutes ...`, Just not always full data – Edgar Jan 20 '16 at 09:58
  • @Edgar I have updated again my answer trying to manage inputs that you described – VincenzoC Jan 20 '16 at 11:36
0

You can create a wrapper for this.

Note: when you do new Date(), you will get time as well, but when done like new Date(yyyy,mm,dd), no time is added and time is always 00:00:00

function createYearStartDate(year){
  return new Date(year, 0,1);
}

(function(){
  var d1 = createYearStartDate("2010")
  console.log(d1)
})()
Rajesh
  • 24,354
  • 5
  • 48
  • 79