0

I have a time stamp retrieved from Firebase which was generated from ionic mobile application and stored in Firebase.and I wanted to print it in my website.

Here's my timestamp from firebase timeStamp: 1516791866433;

Assuming that i already retrieved the said data, how do i seperate date and time?

Jennica
  • 327
  • 1
  • 3
  • 15

3 Answers3

0

new Date(timeStamp) will return a date object => Wed Jan 24 2018 06:04:26 GMT-0500 (EST)

to separate Date and Time, use Date methods: https://www.w3schools.com/jsref/jsref_obj_date.asp

there are many different standards for expressing date, so once you find the one that returns the format you want you can use it like so:

const date = new Date(1516791866433);
const ISOstring = date.toISOString();
const localeString = date.toLocaleString();
const timeStringOnly = date.toTimeString();

console.log(localeString)
console.log(timeStringOnly)
CodeIt
  • 3,492
  • 3
  • 26
  • 37
Pdeona
  • 1
  • 1
0

It's easy just pass the timestamp to Date object.

var datetime = new Date(1516791866433)
var date = datetime.toDateString()
var time = datetime.toTimeString()
console.log(date)
console.log(time)

Another Method:

var date = new Date(1516791866433);
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).substr(-2);
var day = ("0" + date.getDate()).substr(-2);
var hour = ("0" + date.getHours()).substr(-2);
var minutes = ("0" + date.getMinutes()).substr(-2);
var seconds = ("0" + date.getSeconds()).substr(-2);

var d = year + "-" + month + "-" + day + " ";
var t = hour + ":" + minutes + ":" + seconds;

console.log(d)
console.log(t)
CodeIt
  • 3,492
  • 3
  • 26
  • 37
0

This is the unix time and you need to convert it into human time.

// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(1516791866433*1000);

var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();

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

This will convert the unix time into human time.

Hiten
  • 724
  • 1
  • 8
  • 23