I am getting table data from json,Here date is displaying as1238626800000.
How can I convertit to normal date format like10-02-2017
code: https://jsfiddle.net/ncdpw7cf/
Any help?
-
How do I ask a good question?: "If it is possible to create a live example of the problem that you can link to (for example, on sqlfiddle.com or jsbin.com) then do so - but also include the code in your question itself. Not everyone can access external sites, and the links may break over time."Andreas– Andreas2017-04-17 12:17:49 +00:00Commented Apr 17, 2017 at 12:17
3 Answers
What you have as the number "1238626800000" is a UNIX timestamp (i.e. the number of miliseconds since 1 January 1970).
JavaScript Date object recognizes this, so simply try:
new Date(parseInt(leaveList[i].leaveOn))
instead of
leaveList[i].leaveOn
new fiddle here: https://jsfiddle.net/zLaoc00x/1/
In order to format it so it appears as "12-02-2017", you can use a function:
function unix_to_readable(timestamp) {
var date = new Date(parseInt(timestamp)) ;
return ('0' + date.getDate()).slice(-2) + '-'
+ ('0' + (date.getMonth()+1)).slice(-2) + '-'
+ date.getFullYear();
}
Second fiddle here: https://jsfiddle.net/7534hwee/2/
5 Comments
10 means how can I add 0 like 05-02-2017.slice(2) but i am unable to understand this .slice(-2).Actually array starts from 0 rite? then what is .slice(-2)You can convert your timestamp string to a JavaScript date object, then use the toISOString method to create your formatted date like this:
function formatDate(inputStr) {
var timestamp = parseInt(inputStr, 10);
var date = new Date(timestamp);
return date.toISOString().substr(0, 10);
}
console.log(formatDate("1238626800000"));
See updated fiddle: https://jsfiddle.net/pahund/tytzuckz/1/
1 Comment
return day+'/'+month+'/'+year; this methodThis helps me to convert json data to date format..
function formatDate(inputStr) {
var timestamp = parseInt(inputStr, 10);
var date = new Date(timestamp);
var a =date.toISOString().substr(0, 10);
var datePart = a.match(/\d+/g),
year = datePart[0].substring(2),
month = datePart[1], day = datePart[2];
return day+'-'+month+'-'+year;
}