Skip to content
Advertisement

How to manage datetime in angular

I’m working with Angular(v5). I have some problem with Datetime.

I need the current time and I have to save it in a variable. After that I have to subtract an interval of hours (8 hours or an hour) and save the result in a variable and then do it withconsole.log.

I need the format to be: YYYY-MM-DD HH:mm:ss

I tried to integrate moment.js without fail but I always get errors in the console, how can I solve?

I upload my code here

is there a way to manage datetime in angular simply?

thanks

Advertisement

Answer

your import statement is incorrect
change statement to:

import moment from 'moment';

your desired format can be achieved with the following statement:

time.format('YYYY-MM-DD HH:mm:ss')

I forked your code and added the formating and your time calculation functions:
time format example

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement