Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  57] [ 7]  / answers: 1 / hits: 28764  / 8 Years ago, fri, december 2, 2016, 12:00:00

I have epoch time stamp value and I want to extract the Time from it.
For example: input 1480687432 i.e.(Fri Dec 02 2016 14:03:52 GMT+0530 (IST)) output 14:03:52, I want to compare it with sunset/sunrise time (for future surrise/sunset timings also) to find out whether it is day or night. I am using below approach, can anyone please suggest the better approach than this in javascript of in moment.js





var input = 1480687432; //  i.e.(Fri Dec 02 2016 14:03:52 GMT+0530 (IST))` 
// output 14:03:52

function getTimeFromDate(timestamp) {
var date = new Date(timestamp * 1000);
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

var time = new Date();
return time.setHours(hours, minutes, seconds);
}
console.log(getTimeFromDate(1480687432))




More From » date

 Answers
39

Javascript has an in-built function for this. You can use any of the below based on your need.


var timestamp = 1480687432 * 1000;
console.log(new Date(timestamp).toTimeString());
console.log(new Date(timestamp).toLocaleTimeString());

[#59835] Wednesday, November 30, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tylerdamiena

Total Points: 139
Total Questions: 90
Total Answers: 118

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
tylerdamiena questions
;