Convert string to Date without considering timezone - Typescript

javascript date without timezone
timezone-js
moment js convert to local timezone

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?

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));

toDate() creating incorrect Date object · Issue #309 · moment , toDate() produces a Date of "Mon Apr 24 2017 22:00:00 GMT-0600 (Mountain angular filters for moment objects without converting them to Date objects. My guess is that datetimepicker isn't able to parse the -0600 out of current timezone You might consider one that's designed with moment in mind. String and Date manipulations in typescript. string and date are of objects in typescript which holds different values. Every application, we want to manipulate dates and convert to/from date to different types. Date objects have default current date and time in typescript. Date with default constructor returns current date time.

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'}))

moment('YYYY-MM-DD') should be always set in UTC time zone , valueOf() and Date.parse('2013-10-06') do not result in the same time I guess a date-only string without time zone follow this rule, and some  The Date object is the key to date and time functionality in TypeScript. If we create it with no argument passed to its constructor, it will contain the current date and time of the user's computer. The Date object also provides a number of functions dealing with something called Coordinated Universal Time (UTC) time, also known (in the Winter

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

Tips and tricks for date handling with moment.js, Handling dates when dealing with timezones is a difficult task. encountered several issues in displaying dates in the correct format and timezone time in every part on the world corresponding to the timezone without DST. TypeScript Date Object. The Date object represents a date and time functionality in TypeScript. It allows us to get or set the year, month and day, hour, minute, second, and millisecond. If we create a date without any argument passed to its constructor, by default, it contains the date and time of the user's computer.

Handling Time Zone in JavaScript - TOAST UI, I pretty well knew that the time zone support in JavaScript is… Many consider GMT and UTC the same thing, and the two are used As I mentioned earlier, we use the names of time zones (KST, JST) interchangeably with offset without However, as described in the MDN's Date.parse() Document, the  DateStr – A Strongly-Typed Date String for TypeScript Nearly every project I have ever worked on has had to deal with dates in one way or another. For example, there might be a need to generate a report for a specific date range.

Guides, //date math moment('2016-03-12 13:00:00').add(1, 'day').format('LLL') "March A time zone usually has more than one offset from UTC due to daylight saving time. Effectively what this means is that ISO 8601 dates without an offset are to be it is important to consider whether it is more important that dates be accurate,  JavaScript only has a Date object, which is misnamed since it is really a date+time. Which means it cannot parse a time string HH:mm:ss without a date, but it can parse a date string. There are a few options. Convert time to a datetime string and parse using Date(). Use Momentjs String + Format parsing function. Using Date()

Javascript Date Without Time Zone, When getting a date, without specifying the time zone, the result is converted to the Timezone specific / local string to local Date object in JavaScript. that empowers you to build components using vanilla JavaScript or TypeScript. Looking at the access logs to my website, one of the most popular popular pages is  The parse () method takes a date string (such as "2011-10-10T14:48:00") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the setTime () method and the Date object. Date Time String Format.

Comments
  • You can use moment.utc("11/10/2015 10:00:00", "MM/DD/YYYY HH:mm:ss"), do you want to a native JavaScript solution?
  • @VincenzoC Date.UTC() does the same thing natively
  • works, thank you!
  • 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 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?
  • The OP says "it prints", not "I want". You might be correct, but it's not certain.
  • Sorry for not being clear, I meant that I expected:"Tue Nov 10 2015 10:00:00" (without "+0200")
  • "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.