1

My service is returning this as date 7/14/2016 2:40 AM +00:00. How can I convert this to UTC in JS?

Tried:

new Date("7/14/2016 2:40 AM +00:00").toISOString();

In database the date has been stored as UTC so when I will display the date I want to display as Local time.

10
  • 1
    You're over-thinking this... Commented Jul 14, 2016 at 20:31
  • 2
    Have you tried momentjs before? Commented Jul 14, 2016 at 20:32
  • 1
    @AlonEitan why does everyone mention that in every single js date question? Adding a complex library that weighs in at 12+kb gzipped to parse a date string is a bit much. Commented Jul 14, 2016 at 20:35
  • 2
    @JaredSmith Simply because it helps handling the dates aspect much easier, and what's 12+kb gzipped (probably less if you're using the minified version) when you have websites full of images and other visuals Commented Jul 14, 2016 at 20:40
  • 1
    @JaredSmith I totally agree - It basically depend on how often you need to manipulate the dates and how complex that task is. I think it's just like jQuery (or maybe angularjs and other similar frameworks) - You'll use it for complex DOM manipulation, but you don't have to use it it when all you need is document.getElementById() Commented Jul 14, 2016 at 20:57

3 Answers 3

1

There are many ways to parse a string to produce a Date object.

  • One way is with the Date object itself, either by passing a string to the constructor, or by using Date.parse. However, only the ISO8601 formats are required in the ECMAScript specification. Any other input is implementation specific, and may or may not be recognized by the different JavaScript runtimes. With web browsers in particular, there are many differences in supported formats across browsers.

    Additionally, the locale of the environment plays a factor in how the values are parsed. How should 1/2/2016 be parsed? January 2nd, or February 1st? You showed an example of 7/14/2016, which would be invalid input if ran in the UK (for example), where the date format is DD/MM/YYYY.

  • You can write custom code to split the string up into its parts, parse each part individually, and compose a result. The main problem with this approach is that the code tends to be rigid, and sometimes fragile. It should be well tested, and many edge cases need to be considered.

  • You can use a library, which is by far the easiest and most flexible approach (IMHO). With a good library, you can take comfort in the shared experiences of others, and in the unit tests that are (hopefully) part of the library you choose. Of course, using a library comes with several tradeoffs including increased file size, and relinquishing some degree of control. You should evaluate these tradeoffs carefully.

    There are many date libraries available for JavaScript. The most popular is probably moment.js, though there are others to choose from, and some larger frameworks sometimes have similar functionality already included.

    Here is an example using moment.js:

    var i = "7/14/2016 2:40 AM +00:00";
    var f = "M/D/YYYY h:mm A Z";
    var m = moment(i, f);
    var o = m.format(f); // will be in the local time, in the same format as the input
    var d = m.toDate();  // if you really want a Date object
    
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you can guarantee that format for all dates, the following code will suffice:

const datetime = '7/14/2016 2:40 PM +00:00'; // this is what your service returns
const pieces = datetime.split(/[/: ]/);

if (pieces[3] == 12) pieces[3] = 0; // fixes edge case for 12 AM/PM

const hours = pieces[5] === 'PM' ? Number(pieces[3]) + 12 : pieces[3];
const d = new Date(Date.UTC(pieces[2], pieces[0] - 1, pieces[1], hours, pieces[4]));

console.log(datetime); // prints "7/14/2016 2:40 PM +00:00"
console.log(d); // prints Date 2016-07-14T14:40:00.000Z

EDIT: There's a couple edge cases with this not handled correctly, namely 12 AM/PM, etc. but those can easily be worked around as well.

EDIT2: Accounted for that edge case.

EDIT3: As a comment stated, this will only work for UTC times. If the string you're receiving can have any offset, this will not work.

6 Comments

This doesn't account for any offset other than +00:00.
12 am/pm is hardly an "edge" case. In an evenly distributed random sample it will occur about 8% of the time.
If you really want to argue about my definition of edge case, go for it (: I already edited the answer to account for it, so it's a bit moot at this point...
@MattJohnson, yes, I am making an assumption the OP is receiving the strings in UTC since the example was. If the times can be in any offset my answer will not work. I've added a clarification so people know if they read my answer it only applies to UTC.
@michel I tried to replace with UTC and then converted this to a new Date and it worked for me in IE also. Tried the solution you provided but this was throwing errors in IE.
|
0
var str = "7\/15\/2016 1:00 AM +00:00".replace("+00:00","UTC");
console.log(new Date(str).toISOString()); // 2016-07-15T01:00:00.000Z

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.