1

I'm getting a date as a string, in the following format (for example):

"11/10/2015 10:00:00"

This is UTC time.

When I create Date from this string, it consider it as local time:

let time = "11/10/2015 10:00:00";
let date = new Date(time); 
console.log(date);

it prints:

"Tue Nov 10 2015 10:00:00 GMT+0200"

(instead of considering it as UTC: "Tue Nov 10 2015 10:00:00")

I also tried moment.js for that.

is there a good way to make Date() consider the string a UTC, without adding "Z"/"UTC"/"+000" in the end of the string?

Aviram Adiri
  • 85
  • 2
  • 7

4 Answers4

3

You can use the built-in Date.UTC() function to do this. Here's a little function that will take the format you gave in your original post and converts it to a UTC date string

let time = "11/10/2015 10:00:00";

function getUTCDate(dateString) {
  // dateString format will be "MM/DD/YYYY HH:mm:ss"
  var [date, time] = dateString.split(" ");
  var [month, day, year] = date.split("/");
  var [hours, minutes, seconds] = time.split(":");
  // month is 0 indexed in Date operations, subtract 1 when converting string to Date object
  return new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds)).toUTCString();
}

console.log(getUTCDate(time));
mhodges
  • 10,938
  • 2
  • 28
  • 46
  • You're assuming month/day/year format, but given the OP was posted at 2018-10-10 15:36:59Z, it's more likely to be the much more common day/month/year format. ;-) – RobG Oct 10 '18 at 20:42
  • @RobG OP wants "Nov 10th 2015" to spit out, correct? With an input of `"11/10/2015"` that's exactly what you'll get. For that reason, I assumed it was "month/day/year", unless I'm missing something? – mhodges Oct 10 '18 at 21:01
  • The OP says "*it prints*", not "*I want*". You might be correct, but it's not certain. – RobG Oct 10 '18 at 22:19
  • Sorry for not being clear, I meant that I expected:"Tue Nov 10 2015 10:00:00" (without "+0200") – Aviram Adiri Oct 11 '18 at 07:15
2

Your date is parsed by the date constructor, in MM/DD/YYYY format, applying the local timezone offset (so the output represents local midnight at the start of the day in question). If your date really is MM/DD/YYYY, all you need to do is subtract the timezone offset and you'll have the UTC date...

var myDate = new Date("11/10/2015 10:00:00");
myDate = new Date( myDate.getTime() - (myDate.getTimezoneOffset()*60*1000));
console.log(myDate.toLocaleString([],{timeZone:'UTC'}))

Here's everything I know about managing timezone when serializing and deserializing dates. All the timezone handling is static! JS dates have no intrinsic timezone.

bbsimonbb
  • 27,056
  • 15
  • 80
  • 110
0

You can use Date.UTC for this but you will have to parse your string and put every part of it as args by yourself as it can't parse such string. Also you could use moment.js to parse it: Convert date to another timezone in JavaScript

Also, seems like new Date("11/10/2015 10:00:00 GMT") parses date as a GMT and only after that converts it to PC local time

Eugene Tsakh
  • 2,777
  • 2
  • 14
  • 27
  • 1
    "*Also, seems like…*" Worth noting that "11/10/2015 10:00:00 GMT" is not a format supported by EMCA-262 so parsing is implementation dependent. It's also ambiguous, most people will recognise it as 11 October, but parsers may well treat it as 10 November. – RobG Oct 10 '18 at 20:40
0

Easy answer is to append a "Z" at the end without changing the variable:

let time = "11/10/2015 10:00:00";  
let date = new Date(time + "Z");  
console.log(date);
Christophe
  • 597
  • 4
  • 14