You need to explicitly tell JavaScript that the date you are storing is currently in UTC (which is the same a GMT). You do this by appending Z
, which stands for Zulu Time. If you don't specify it, it falls back to the browser's implementation of the format, which is inconsistent.
var createDate = '2016-07-12 09:09:38Z';
createDate = createDate.replace(" ", "T");
createDate = new Date(createDate);
When you log the variable to the Console, you may notice differences between the Date/Time shown still. It's important to note that the Date/Time is correct. Again, it relates to the browser's implementation of the formatting. You can confirm that the internal values are correct with:
console.log(createDate.toString())
console.log(createDate.toUTCString())
The toString()
will return the Date/Time in the local Time Zone, and the toUTCString()
will return it in UTC/GMT, which should match the value you created the object with.