0

I have been following Convert a Unix timestamp to time in JavaScript thread for answer but looks like single digit time (0-9) is parsed as it is. The accepted answer

// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp*1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();

// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);

We get like 2:3:9 instead of 02:03:09. How to get rid of this behaviour? Also can anyone please elaborate on how to get am/pm along with time?

2 Answers 2

1
var formattedTime = ('0' + hours).substr(-2) + ':'
                + ('0' + minutes).substr(-2) + ':' 
                + ('0' + seconds).substr(-2);

I think I will leave the am:pm bit to you. Press ctrl-shift j and play with your code in the console right here

//                                 /*Year m-1 d h m s ms*/
unix_timestamp = Math.floor(new Date(2016,0,  1,5,5,0,0)/1000)

This might be easier to understand. I have kept it closer

// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp*1000);
// Hours part from the timestamp
var amPm = date.getHours() >= 12?'AM':'PM'
// % is modulo which is the remainder after division || will change 0 to 12
// because 0 is falsey everything else will be left as it is
var hours = ("0" + ((date.getHours() % 12)||12)).substr(-2)
// Minutes part from the timestamp
var minutes = ("0" + date.getMinutes()).substr(-2)
// Seconds part from the timestamp
var seconds = ("0" + date.getSeconds()).substr(-2)

// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes+ ':' + seconds + ' '+ amPm
Sign up to request clarification or add additional context in comments.

2 Comments

This works like charm but I have a small query. How does it understand if it has to append a 0 or no. For values less than 9 it should append and for more than 10 it should not.
the .substr takes the last 2 chars in the string so 10 am becomes the string '010' which becomes '10'. I will post a better answer for you because I should make it match the way your example was posted, as the only problem with the original is that no 0 was pre-pended for the hours.
0

I think you have to get rid of the substr-part, since the value should already be correct.

Note: you need to check if the values are already above 9, because you don't need to append anything when it is above 9.

Example

var d = new Date() //Is in milliseconds

var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();

console.log(hours + ":" + ((minutes < 10) ? "0" + minutes : minutes) + ":" + ((seconds < 10) ? "0" + seconds : seconds))

I want to add that these kinds of problems can be easily resolved with using a good library like moment.js

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.