Reputation: 1358
I want to convert a string with just the date part, into a Date object in nodejs. If I do this:
console.log('2010-10-05 ||', new Date('2010-10-05'));
console.log('2010-10-05 00:00:00 ||', new Date('2010-10-05 00:00:00'));
I obtain this in console:
2010-10-05 || Mon Oct 04 2010 19:00:00 GMT-0500 (CDT)
2010-10-05 00:00:00 || Tue Oct 05 2010 00:00:00 GMT-0500 (CDT)
I don't want '2010-10-05' to be converted into '2010-10-04' because of my timezone. My timezone is -0500 GMT.
How can I create a date by just providing the Date part without the gap ?
Upvotes: 0
Views: 1007
Reputation: 12265
console.log('2010-10-05 ||', new Date('2010-10-05' + ' UTC'))
console.log('2010-10-05 00:00:00 ||', new Date('2010-10-05 00:00:00' + ' UTC'))
Upvotes: 1
Reputation: 318192
Use zeros for hour, minute, second etc.
var date = '2010-10-05',
arr = date.split('-'),
obj = new Date(arr[0], arr[1], arr[2], 0, 0, 0, 0);
Date.UTC
uses universal time instead of the local time, if that's what you need
Date.UTC(arr[0], arr[1], arr[2], 0, 0, 0, 0)
Upvotes: 1