Gettin local time


#1

HI!

Im not sure if this is an Ionic topic or it should be moved to ngCordova or other…
Im makin a chat app, I am able to push the messages to Firebase but I dont know how can I add the local time…
For example:

message: Hi! This is a message!
time: 29/06/2017 19:45hs

Do I need a plugin to get the time of the device or is there another way just with typescript or Ionic?

Thanks!


#2
let date = new Date();

console.log(date);

This will give you the date, I’d recommend storing the date in milliseconds though.


#3

I’d recommend checking out the moment.js library.

Instructions on how to use the moment.js library with Typescript are here.

So in order to create a timestamp like the one you mentioned, you would do this:

import * as moment from 'moment';

let now = moment().format('DD/MM/YYYY HH:mm:ss');

#4

Thanks! Loved that library!


#5

I’d recommend the datefns library over Moment. It’ll work better in Ionic because it will only import the stuff you use, versus Moment which will important everything which will naturally increase your overall bundle size.