0

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?

5 Answers5

1

I was able to solve my problem using the below code:

function DateConvert(JsonDate) {
    var date = new Date(parseInt(JsonDate.substr(6)));
    date = date.toLocaleString('en-US', {
        hour: 'numeric',
        minute: 'numeric',
        hour12: true
    });
    return date;
}
Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
0

Try this

yourdateObj.toLocaleFormat('%H:%M:%S')

EDIT : This works only in firefox.

Akhil
  • 2,602
  • 23
  • 36
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)

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

How about Date#toLocaleTimeString with proper locale?

console.log(new Date().toLocaleTimeString('in')); 
barbsan
  • 3,418
  • 11
  • 21
  • 28
0
Try This..
var dateString = "/Date(1623781800000+0530)/"+.substr(6);
var currentTime = new Date(parseInt(dateString));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
  if (month.toString().length == 1) 
     month = "0" + month.toString();
  if (day.toString().length == 1){ 
     day = "0" + currentTime.getDate();}                                       
var datenew = day + "/" + month + "/" + year;
  • Please provide more informations than just the code you've provided. For example, in what browser did you test this solution? What are it's potential limitations? Why is this better than the other answers already provided? Cheers. – Ad5001 Jul 28 '21 at 09:50