Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JavaScript to Parse Time

This is probably something easy, but I'm a bit confused how to do this. How can I, using JavaScript, parse only the time from the following ISO 8601 date string:

 2009-12-06T17:10:00

In other words, with the string above, I'd like to output:

5:10 PM

Any guidance/tutorials on this would be great.

like image 311
Dodinas Avatar asked Dec 06 '09 18:12

Dodinas


People also ask

Can JavaScript handle dates and time?

The Date object is a built-in object in JavaScript that stores the date and time. It provides a number of built-in methods for formatting and managing that data. By default, a new Date instance without arguments provided creates an object corresponding to the current date and time.

What data type is time in JavaScript?

JavaScript does not have a date data type. However, you can use the Date object and its methods to work with dates and times in your applications. The Date object has a large number of methods for setting, getting, and manipulating dates.

How does parse work in JavaScript?

Parsing means analyzing and converting a program into an internal format that a runtime environment can actually run, for example the JavaScript engine inside browsers. The browser parses HTML into a DOM tree. HTML parsing involves tokenization and tree construction.

How do you convert a string to a Date in JavaScript?

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object.


1 Answers

Chrome & Firefox: Standard JavaScript Date constructor takes ISO 8601 date string. For example:

var sampleDate = new Date("2010-03-07T02:13:46Z");

Returns this Date object: "Sun Mar 07 2010 13:13:46 GMT+1100 (AUS Eastern Daylight Time)"

This does not work in IE (including latest IE 9)

Here is a cross-browser solution by Paul Sowden at http://delete.me.uk/2005/03/iso8601.html :

Date.prototype.setISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    this.setTime(Number(time));
}

Usage:

var date = new Date();
date.setISO8601("2005-03-26T19:51:34Z");

If you do a lot of datetime manipulation in JavaScript, I can also suggest to check some JS libraries like MomentJS. It handles some common things like date parsing, formatting and calculating difference between two dates and has support for multiple localizations.

like image 105
Maksym Kozlenko Avatar answered Sep 17 '22 18:09

Maksym Kozlenko