0

I have a date 12/19/2016 8:54 in a variable. I want the value of only the time without the date. How do I do this?

Examples:

  • Input: 12/19/2016 8:54 Output: 8:54

  • Input: 2/2/2018 11:30 Output: 11:30

The only way I see this is string manipulation, which I believe is a bad idea.

Akhil Kintali
  • 496
  • 2
  • 11
  • 27
  • 3
    `'12/19/2016 8:54'.split(' ')[1]` – Hassan Imam Feb 03 '21 at 10:23
  • Why is manipulating a string a bad idea? – Jeremy Thille Feb 03 '21 at 10:24
  • @JeremyThille While working with dates, string manipulation is rarely suggested. – Akhil Kintali Feb 03 '21 at 10:31
  • But a Date object is an object. What about string manipulation when working with an object? I don't see the relation – Jeremy Thille Feb 03 '21 at 10:34
  • @JeremyThille Although the given solution works, I felt that any JS library would have been more suitable. – Akhil Kintali Feb 03 '21 at 10:37
  • Any JS library to manipulate strings when working with Date objects? :) You're losing me more and more. Why would you need a library to split a string? – Jeremy Thille Feb 03 '21 at 10:44
  • @JeremyThille Not to split the string. Rather to work with the Date. – Akhil Kintali Feb 03 '21 at 10:48
  • 1
    If you want to parse the timestamp to a Date, that is covered elsewhere, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). Using the built–in parser per the answer you've accepted is not not recommended for strings in formats not supported by ECMA-262. – RobG Feb 03 '21 at 22:08

2 Answers2

3

you can use split method.

const time = '12/19/2016 8:54'.split(" ")[1];
kunal panchal
  • 798
  • 5
  • 21
1

If your date variable is an actual Date object then you can use Intl.DateTimeFormat to get it formatted any way you want. Here is how:

var d = new Date(2016, 11, 19, 8, 54);
console.log(d); // If you are not in UTC then this will show the Date with timezone adjustment applied

var hourAndMinute = { hour: "numeric", minute: "numeric" };
var formatter = new Intl.DateTimeFormat("en-GB", hourAndMinute);
var result = formatter.format(d); // This does the reverse timezone adjustment
console.log(result);
Peter B
  • 22,460
  • 5
  • 32
  • 69
  • `new Date("2016-12-19 08:54")` returns an invalid date in Safari, so the above returns *null* and a RangeError. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). Formatting of dates is a duplicate of many, many other questions. The formatter doesn't reverse anything, the timestamp doesn't have an offset so if parsed to a valid date, the local offset will be used. Absent the *timeZone* option, the formatter will also use the local offset. – RobG Feb 03 '21 at 21:58
  • The `new Date("2016-12-19 08:54")` is only a quick way to get a date object in my script. I changed it to `new Date(2016, 11, 19, 8, 54)`. Even then, the log() command still prints it with a time difference from what I set it to (7:54 in my case). To me that is a timezone adjustment, if it isn't then what is it? – Peter B Feb 03 '21 at 22:21
  • A bug? The values are parsed as local and the output formatted string is also local, the same offset should be used for both operations as one is just the reverse of the other. – RobG Feb 03 '21 at 22:29