-2

I'm using new Date("2017-01-12") for string to date conversion. Its working fine for IST, If I change the device time zone like Chicago(CST), then the date shows as previous date. Because CST is GMT-0600 so it reduces -6 hours and shows previous day date("2017-01-11"). Any other way to convert the date string to date using Moment?

Manikandan K
  • 117
  • 14

1 Answers1

2

Date(year, month, day) is the valid constructor here. 2 things to know, when you pass the month it starts in 0, so if you pass January it is the month 0, don't to confuse with 1. Then if you have a string format date like YYYY-MM-YY you have to do this supposing that your string is named stringDate:

var stringDateSplited = stringDate.split('-');
var dateResult = new Date(stringDateSplited[0], (stringDateSplited[1] -1) , stringDateSplited[2]);

This could be the result you want!

SmoggeR_js
  • 2,950
  • 4
  • 22
  • 52