46

The following:

new Date(1324339200000).toUTCString()

Outputs:

"Tue, 20 Dec 2011 00:00:00 GMT"

I need it to return Dec 20. Is there a better method I can use besides toUTCString()? I am looking for any way to parse through milliseconds, to return a human readable date.

Christian Fazzini
  • 19,613
  • 21
  • 110
  • 215

7 Answers7

51

Using the library Datejs you can accomplish this quite elegantly, with its toString format specifiers: http://jsfiddle.net/TeRnM/1/.

var date = new Date(1324339200000);

date.toString("MMM dd"); // "Dec 20"
pimvdb
  • 151,816
  • 78
  • 307
  • 352
50

You can use datejs and convert in different formate. I have tested some formate and working fine.

var d = new Date(1469433907836); // Parameter should be long value

d.toLocaleString()     // 7/25/2016, 1:35:07 PM
d.toLocaleDateString() // 7/25/2016
d.toDateString()       // Mon Jul 25 2016
d.toTimeString()       // 13:35:07 GMT+0530 (India Standard Time)
d.toLocaleTimeString() // 1:35:07 PM
d.toISOString();       // 2016-07-25T08:05:07.836Z
d.toJSON();            // 2016-07-25T08:05:07.836Z
d.toString();          // Mon Jul 25 2016 13:35:07 GMT+0530 (India Standard Time)
d.toUTCString();       // Mon, 25 Jul 2016 08:05:07 GMT
Ravindra Kumar
  • 1,842
  • 14
  • 23
18

No, you'll need to do it manually.

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();
}

console.log(prettyDate(new Date(1324339200000)));
Nhan
  • 3,595
  • 6
  • 30
  • 38
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
6

This is a solution. Later you can split by ":" and take the values of the array

 /**
 * Converts milliseconds to human readeable language separated by ":"
 * Example: 190980000 --> 2:05:3 --> 2days 5hours 3min
 */
function dhm(t){
    var cd = 24 * 60 * 60 * 1000,
        ch = 60 * 60 * 1000,
        d = Math.floor(t / cd),
        h = '0' + Math.floor( (t - d * cd) / ch),
        m = '0' + Math.round( (t - d * cd - h * ch) / 60000);
    return [d, h.substr(-2), m.substr(-2)].join(':');
}

//Example
var delay = 190980000;                   
var fullTime = dhm(delay);
console.log(fullTime);
ssamuel68
  • 932
  • 13
  • 10
4

You can do it with just a few lines of pure js codes.

var date = new Date(1324339200000);
    var dateToStr = date.toUTCString().split(' ');
    var cleanDate = dateToStr[2] + ' ' + dateToStr[1] ;
console.log(cleanDate);

returns Dec 20. Hope it helps.

Koushik Das
  • 9,678
  • 3
  • 51
  • 50
2

I just tested this and it works fine

var d = new Date(1441121836000);

The data object has a constructor which takes milliseconds as an argument.

Yurets
  • 3,999
  • 17
  • 54
  • 74
user3669653
  • 354
  • 1
  • 8
  • 15
1

Building on lonesomeday's example (upvote that answer not this one), I ran into this output:

undefined NaN, NaN

var datetime = '1324339200000'; //LOOK HERE

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();
}

console.log(prettyDate(new Date(datetime))); //AND HERE

The cause was using a string as input. To fix it, prefix the string with a plus sign:

prettyDate(new Date(+datetime));

var datetime = '1324339200000';

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();
}

console.log(prettyDate(new Date(+datetime))); //HERE

To add Hours/Minutes to the output:

var datetime = '1485010730253';

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] +' '+ date.getUTCDate()+ ', '+ date.getUTCHours() +':'+ date.getUTCMinutes();
}

console.log(prettyDate(new Date(+datetime)));
Community
  • 1
  • 1
crashwap
  • 2,846
  • 3
  • 28
  • 62