Search code examples
javascriptnode.jsdatetimeepoch

Display epoch time as HHMM format in javascript


I have a variable that contains epoch time. I want to display the time in HHMM format.

Here is my code;

function epoch_time_to_date_obj(epoch_time)
{
    var utcSeconds = epoch_time;
    var d = new Date(0); 
    d.setUTCSeconds(utcSeconds);    
    return d;
}

let epoch_time = 1234567890;
let date_obj = epoch_time_to_date_obj(epoch_time);

I would like to extract the HHMM time information from date_obj and display the time in HHMM format in Hong Kong time.

I am using node.js v6


Solution

  • let epoch_time = 1234567890 * 1000;
    var date_obj = new Date(epoch_time);
    const hrs = date_obj.getHours();
    const mins = date_obj.getMinutes();
    let hhmm = (hrs < 10 ? "0" + hrs : hrs) + ":" + (mins < 10 ? "0" + mins : mins);
    
    alert(hhmm);