2

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?

1
  • 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." Commented Apr 17, 2017 at 12:17

3 Answers 3

6

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/

Sign up to request clarification or add additional context in comments.

5 Comments

Unix time: "Unix time (also known as POSIX time or epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds."
If it is less than 10 means how can I add 0 like 05-02-2017
@user7397787 try the 2nd fiddle once again, it should formatting as you want (with leading zeros) - jsfiddle.net/7534hwee/2
One last doubt I know the meaning of .slice(2) but i am unable to understand this .slice(-2).Actually array starts from 0 rite? then what is .slice(-2)
negative value means it starts from the end and moves backwards. there is a good example here of this: developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
3

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

Here how can I get return day+'/'+month+'/'+year; this method
1

This 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;

 }


https://jsfiddle.net/tytzuckz/2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.