This is a simple guide about how to install moment
package and use it in an Ionic app, in both typescript files and view (html) files.
npm install moment --save-prod
npm install moment-timezone --save-prod
import * as moment from 'moment-timezone';
Note that we import the moment-timezone
package not the moment
package because this way we can use the methods of the moment-timezone
package as well as the methods of the moment
package because the moment-timezone
package exports them.
export class ClassName {
momentjs: any = moment;
//..
//..
}
// Set the default timezone to UTC
// More info about moment timezone: http://momentjs.com/timezone/docs
this.momentjs.tz.setDefault('UTC');
// Current datetime according to the default timezone (UTC as determined above)
let currentDateTime = this.momentjs().format('YYYY-MM-DD HH:mm:ss ZZ');
console.log(currentDateTime);
// A specific datetime according to a specific timezone ('Africa/Cairo' in this example) other than the default one (UTC as determined above)
let dateTimeAccordingToAnotherTimezone = this.momentjs('2018-10-15 15:59:59').tz('Africa/Cairo').format('DD-MM-YYYY @ hh:mm a ZZ');
console.log(dateTimeAccordingToAnotherTimezone);
<p>Order Placed at: {{ momentjs(order.created_at).tz('Africa/Cairo').format('DD-MM-YYYY @ hh:mm a') }}</p>
This question and answer also could be found on stackoverflow.com on this link