1

My application receives a "dateTtime±timezone" from the API. I'm supposed to show that exact time while formatted with the user defined preference:

var userFormat = "en-US"
var original = "2020-09-01T12:14:05.663-01:23" // strange timezone only to make a point
var date = new Date(original)
console.log(date.toLocaleString(userFormat, {
    hour12:  false   ,
    hour  : "numeric",
    minute: "numeric",
    second: "numeric"}))
// expected: "12:14:05"

How can I get a string with the original 12:14:05, not the local time or the GMT time while avoiding string manipulation?

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • You can't. ECMAScript dates are just a time value, an offset in milliseconds from 1970-01-01T00:00:00Z. They have no concept of timezone. Offset details are retrieved from the host system using [*getTimezoneOffset*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset). On the output side, you can use *toLocaleString* with IANA representative locations for the offset. In any case, you'll have to parse the string yourself to get the offset and apply it. – RobG Sep 01 '20 at 22:49

3 Answers3

1

This may work fine:

  <script type="text/javascript">
        debugger
        var userFormat = "en-US";
        var original = "2020-09-01T12:14:05.663-01:23";
        var time = original.substr(11, 8);
        var HH = time.substr(0, 2);
        var MM = time.substr(3, 2);
        var SS = time.substr(6, 2);
        var date = original.substr(0, 10);
        var YY = date.substr(0, 4);
        var MO = date.substr(5, 2);
        var DD = date.substr(8, 2);

        var Ndate = new Date(YY, MO, DD, HH, MM, SS);

        // sometimes even the US needs 24-hour time
        console.log(Ndate.toLocaleTimeString(userFormat, {
            hour12: false,
        }));
    </script>
  • i would like to avoid string manipulation, if possible; can we get the original value sent to the Date constructor? – Rubens Farias Sep 01 '20 at 18:34
  • Code–only answers aren't that helpful. You should explain why the OP has their issue and how your code fixes it. Also, the month should be `MM - 1` not `MO`. – RobG Sep 01 '20 at 22:52
  • @RubensFarias the Date Constructor only accepts standard inputs which have to be in numeric format, so I think you can override the Date constructor to accept you original format, one way or another, you cannot avoid string manipulation. – Alireza Ali Sep 02 '20 at 09:37
0

If you always find this kind of date string you can then split the string and find the time like as below.

var original = "2020-09-01T12:14:05.663-01:23";
var time = original.split('T')[1].split('.')[0];
Zahid Hasan
  • 527
  • 4
  • 11
  • I would like to avoid string operations; I also need to format it using the user selected locale, so I need to use a Date object ("12:14", "12:14 PM") – Rubens Farias Sep 01 '20 at 18:10
-1

You could trim the timezone from the string before converting it and then use the normal date methods on the result (e.g. toLocaleString()).

var userFormat = "en-US"
var original = "2020-09-01T12:14:05.663-01:23"

// Trim off the timezone
var date = new Date(original.split('.')[0]);

console.log(date.toLocaleString(userFormat, { hour: 'numeric',
  minute: 'numeric',
  second: 'numeric' 
}))

// Expected: "12:14:05 PM"
Ed Lucas
  • 5,955
  • 4
  • 30
  • 42
  • you know, actually the server do not send the timezone but the time shown on some clients are off; we thought introducing the timezone would provide a more reliable solution =/ – Rubens Farias Sep 01 '20 at 19:09
  • You could instead include a timezone string in the options object passed to `toLocaleString()` if you know it, so that it formats the time while respecting the timezone. – Ed Lucas Sep 01 '20 at 19:16
  • This will parse the string as local in most browsers or UTC in some so that it represents a different moment in time for each host with a different offset. – RobG Sep 01 '20 at 22:46