-3

I have a UTC date i.e Sat, 19 Jan 2019 05:40:07 GMT. I want to extract time string from given date i.e 05:40:07. all methods so far I've tried are to get a timestamp. So, I've written my own below code to extract time

var date = new Date('Sat, 19 Jan 2019 05:40:07 GMT'); 
console.log(date.toISOString().split("T")[1].split(".")[0]);

i.e first converted the date into ISO format then split it

Is there any other way to get this without converting it to any other format because my method is correct if there is no direct process to acquire it?

piet.t
  • 11,718
  • 21
  • 43
  • 52
p u
  • 1,395
  • 1
  • 17
  • 30
  • 2
    Your code works just fine, is there a reason why you're looking for an alternative? – CertainPerformance Jan 19 '19 at 05:49
  • yeah, I know it works fine. But its quite weird you see I already have time in UTC format but I am still converting it into ISO then getting what I want.is there any direct method to get time from UTC directly – p u Jan 19 '19 at 05:55
  • 1
    From a Date object you'll *have* to either (1) extract a string, then manipulate it (as you're doing) or (2) extract the needed numbers using Date methods. But IMO your current approach is nicer (much less code, easily understandable), though you can consider using a regular expression instead of multiple `split`s – CertainPerformance Jan 19 '19 at 06:04
  • dont know if it help, but here wrote a little code while back you can take a look https://stackoverflow.com/questions/43416080/javascript-convert-datetime-to-dd-mm-yyyy-time/43416361#43416361 – Alen.Toma Jan 19 '19 at 06:04
  • okay @CertainPerformance then I am answering this question with my approach and closing it..thanks for the help :) – p u Jan 19 '19 at 06:17
  • Why not `'Sat, 19 Jan 2019 05:40:07 GMT'.substr(17,8)`? – RobG Jan 19 '19 at 21:48

4 Answers4

2

So, I couldn't find any other method more appealing than mine :)

var date = new Date('Sat, 19 Jan 2019 05:40:07 GMT'); 
console.log(date.toISOString().split("T")[1].split(".")[0]);
p u
  • 1,395
  • 1
  • 17
  • 30
  • **[My solution](https://stackoverflow.com/a/54264733/9801830)** is a little bit shorter than yours methode. ;-) – Bharata Jan 19 '19 at 06:55
  • @Bharata my question was not to find the shortest method... I wanted a direct solution in which I didn't need to convert it into other forms then extract it :) – p u Jan 19 '19 at 07:01
  • Anyway, now you can use my methode because it is shorter. ;-) – Bharata Jan 19 '19 at 07:05
  • What would you like to say about my new code? See my answer. – Bharata Jan 19 '19 at 07:19
  • still, I'll go with my answer :P thankyou though for help! :) – p u Jan 19 '19 at 07:21
  • @priyanshisrivastavaI you still first convert to a Date object, then format that to a string, then parse the string. Why don't you just parse the string directly, using regular expressions? Or, if you are going to use Date, just use the functions on Date to get the result in the format you need, and avoid the 2nd parsing step. Note you did ask "without converting it to any other format", which this answer is doing. – Adam Millerchip Jan 19 '19 at 12:17
  • Why are you using `new Date` at all? `'Sat, 19 Jan 2019 05:40:07 GMT'.substr(17,8)` does the job more reliably. – RobG Jan 19 '19 at 21:49
1

How about toLocaleTimeString()?

var date = new Date('Sat, 19 Jan 2019 05:40:07 GMT'); 
console.log(date.toLocaleTimeString('en-US', {timeZone: 'UTC', hour12: false}));

If you want to avoid the Date object entirely, you can just parse the string:

var date = 'Sat, 19 Jan 2019 05:40:07 GMT'
var timeRegex = /(\d\d):(\d\d):(\d\d)/
console.log(timeRegex.exec(date)[0])
Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
  • Ah I see. I added a regex that avoids the Date class. Although I think using `Date` is the better choice. – Adam Millerchip Jan 19 '19 at 06:18
  • "*Although I think using Date is the better choice*" why? That means parsing the string using the quirky built-in parser to create a Date, then using *toLocaleString* and the *timeZone* option to maybe get back the same time string that was in the original. – RobG Jan 19 '19 at 12:07
  • Because one day, somebody will ask for the non-UTC time, or some other related requirement, in which case the generic Date answer is useful. But anyway, I provided a regex solution too, which if the UTC date is all that's required, I believe answers the original question. – Adam Millerchip Jan 19 '19 at 12:16
1
   var date = new Date('Sat, 19 Jan 2019 05:40:07 GMT');
   var result=date.getUTCHours()+':'+date.getUTCMinutes()+':'+date.getUTCSeconds();
Sachin Aghera
  • 486
  • 3
  • 8
  • This is good, however using getUTCHours() etc does not give you time in your current time zone, so it might be better to use date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() – Vincent May 30 '22 at 17:50
0

I do not know – is JSON one other format for you? If not then you can do it like follows:

var date = new Date('Sat, 19 Jan 2019 05:40:07 GMT'); 
console.log(date.toJSON().split("T")[1].split(".")[0]);
//or even shorter:
console.log(date.toJSON().slice(-13, -5));

This is a little bit shorter than yours methode.

In other case you can do it like follows:

var dateStr = 'Sat, 19 Jan 2019 05:40:07 GMT'; 
var date = new Date(dateStr); 
console.log(dateStr.slice(-12, -4));
Bharata
  • 13,509
  • 6
  • 36
  • 50