I have a json date like /Date(1334514600000)/ in my response and when I convert it in javascript then I got this date Tue Apr 17 2012 11:37:10 GMT+0530 (India Standard Time), but I need the time format like 11:37:10 and I fail every time. Can anyone tell me how can I resolve it?
Asked
Active
Viewed 149 times
0
-
hint: `'Tue Apr 17 2012 11:37:10 GMT+0530 (India Standard Time)'.split(' ')[4]` – Jaromanda X May 25 '17 at 05:45
-
Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Akhil May 25 '17 at 05:53
-
https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – Akhil May 25 '17 at 05:53
4 Answers
0
Try this
yourdateObj.toLocaleFormat('%H:%M:%S')
EDIT : This works only in firefox.
![](../../users/profiles/1926109.webp)
Akhil
- 2,494
- 20
- 35
-
2
-
@JaromandaX Yes!! . And that was a new Knowledge for me that it is limited to Firefox. Thanks – Akhil May 25 '17 at 05:49
0
The most reliable cross browser way I've found is
new Date().toTimeString().split(' ')[0]
Tested in latest Firefox, Chrome, IE11 (not tested in any other IE) and Edge
To be perfectly honest, it may seem overkill, but if you deal with dates, I recommend not re-inventing the wheel, use something like moment.js
- it has support for timezones as well - it's never let me down (yet)
![](../../users/profiles/5053002.webp)
Jaromanda X
- 47,382
- 4
- 58
- 76
0
How about Date#toLocaleTimeString with proper locale?
console.log(new Date().toLocaleTimeString('in'));
![](../../users/profiles/7404943.webp)
barbsan
- 3,238
- 11
- 18
- 27
0
Finally got the answer:-
function DateConvert(JsonDate) { var date = new Date(parseInt(JsonDate.substr(6))); date = date.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }); return date;
![](../../users/profiles/8063140.webp)
Ajit Dhavale
- 1
- 1