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.
This question is related to
javascript
jquery
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.
Building on lonesomeday
's example (upvote that answer not this one), I ran into this output:
undefined NaN, NaN
var datetime = '1324339200000'; //LOOK HERE_x000D_
_x000D_
function prettyDate(date) {_x000D_
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',_x000D_
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];_x000D_
_x000D_
return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();_x000D_
}_x000D_
_x000D_
console.log(prettyDate(new Date(datetime))); //AND HERE
_x000D_
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';_x000D_
_x000D_
function prettyDate(date) {_x000D_
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',_x000D_
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];_x000D_
_x000D_
return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();_x000D_
}_x000D_
_x000D_
console.log(prettyDate(new Date(+datetime))); //HERE
_x000D_
To add Hours/Minutes to the output:
var datetime = '1485010730253';_x000D_
_x000D_
function prettyDate(date) {_x000D_
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',_x000D_
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];_x000D_
_x000D_
return months[date.getUTCMonth()] +' '+ date.getUTCDate()+ ', '+ date.getUTCHours() +':'+ date.getUTCMinutes();_x000D_
}_x000D_
_x000D_
console.log(prettyDate(new Date(+datetime)));
_x000D_
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.
No, you'll need to do it manually.
function prettyDate(date) {_x000D_
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',_x000D_
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];_x000D_
_x000D_
return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();_x000D_
}_x000D_
_x000D_
console.log(prettyDate(new Date(1324339200000)));
_x000D_
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);
You can use datejs and convert in different formate. I have tested some formate and working fine.
var d = new Date(1469433907836);
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
Source: Stackoverflow.com