0

I'm converting a date string to Unix timestamp in JavaScript.

Here is what I get:

August 26,2015 -- 1440561600

What I should get :

August 26,2015 -- 1440547200

This is obviously happening because of the GMT not being set to 0. So essentially my time is
Wed, 26 Aug 2015 04:00:00 GMT

When it really should convert:
Wed, 26 Aug 2015 00:00:00 GMT

So how can I get rid of the GMT in my date before I use Date.getTime() / 1000 ?

I have tried numerous methods from this website, and none of them have worked. NOTE: I don't want to use a library like Moment.js or anything else. Please let me know.

JohnSnow
  • 6,911
  • 8
  • 23
  • 44
  • Do please search for UTC JavaScript here or in Google. There are duplicates of duplicates here – mplungjan Jan 21 '17 at 08:55
  • I have and none of them work! I just tried another one which suggested to use new Date().getTimezoneOffset() and multiply by 60 to get the difference in milliseconds and subtract that by the original date .... Still doesn't work. Can you just take a few seconds and write a working example? I'm sure it's a piece of cake for you. – JohnSnow Jan 21 '17 at 09:23
  • Nope. Not a piece of cake and I am still not sure what you want exactly - there is UTC and there is ISO dates. Is this not a duplicate? http://stackoverflow.com/questions/9756120/how-do-i-get-a-utc-timestamp-in-javascript – mplungjan Jan 21 '17 at 09:52
  • @JohnNoob - what you described from the other answer is called "epoch shifting" and you have to be *very* careful if you do that, normally you don't want to do that at all. What is confusing us is that you haven't been clear about exactly what your input is and how it is to be interpreted. Do you mean that you have exactly a string `"August 26, 2015"` like that? And if so, do you mean for us to assume you meant to infer `00:00 UTC` on that day? That's fine, but you should state so instead of making that assumption. Also, IMHO, unix time is a poor choice if you are working with whole dates. – Matt Johnson-Pint Jan 22 '17 at 05:49

1 Answers1

0

I think you want to parse a date string like August 26, 2015 with a timezone of UTC+0000, i.e. as 2015-08-26T00:00:00Z. The best way to do that is to format the string correctly and use a reliable parser.

However, if you're going to parse the string to reformat it you might as well also create a Date while you're doing it.

You can't confidently parse the string with the Date constructor or Date.parse since the format is not supported by ECMA-262, so use a library for that or a simple function.

Date strings without a timezone are typically parsed as local, i.e. assuming the host system timezone (except in ECMAScript where, just to be different, ISO 8601 dates like "2017-01-20" are treated as UTC).

To obtain a date as if the string was parsed as UTC, parse it as local then subtract the timezone offset. ECMAScript timezones have the opposite sense to ISO 8601: east is -ve and west is +ve.

Note that Date objects are just a time value, they get the timezone offset from the host. The following manipulates the time value, nothing else (i.e. it doesn't "set" the timezone in any respsect).

e.g.

/* Parse a date in format August 26,2015 as local
** @param {string} s - string to parse in MMMM DD, YYYY format
** @returns {Date}
*/
function parseMdy(s) {
  var months = 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' ');
  var b = s.split(/\W/);
  var month = months.indexOf(b[0].toLowerCase().substr(0,3));
  return new Date(b[2], month, b[1]);
}

// Parse as local
var d = parseMdy('August 26,2015');
console.log('Local date time  : ' + d.toString());

// Subtract timezone
d.setMinutes(d.getMinutes() - d.getTimezoneOffset());
console.log('Adjusted to +0000: ' + d.toISOString());
console.log('UNIX time value  : ' + (d/1000|0));

Due to the quirky specification for ECMAScript parsing, if the date is formatted per ISO 8601 then the built–in parser will treat it as UTC (but this is still somewhat unreliable):

// August 26,2015 formatted as 2015-08-26
var d = new Date('2015-08-26');
console.log(d.toISOString());
console.log(d/1000|0);
RobG
  • 142,382
  • 31
  • 172
  • 209