0

In my app I have a page were can insert a record with a property 'performedOn'. when insert record whit a date(24/07), the time is 00.00am but the GTM is -2.00 infact when save, date change in (23/07 at 22.00pm). How can I change time?? Thank!!!

2 Answers2

0

You need to change the locale or just the timezone.

  • You can change the locale or timezone of your current date object and transform it manually to your needs by using a DatePipe.

First you need to import DatePipe in your app.module file

import { DatePipe } from '@angular/common';

Add it to the providers in @NgModule.

providers: [DatePipe]

In component.ts:

Import into the component.

import { DatePipe } from '@angular/common';

Add it to the constructor.

constructor(private datePipe: DatePipe){}

Then you can do something like this. This would transform the date to the timezone GMT+2.00.

dateObject = new Date();

formattedDate = this.datePipe.transform(this.dateObject, 'full', '+200');

You can see the example live here.

To clarify. DatePipe has 3 arguments (format, timezone, locale) and you can call them like this.

this.myPipe.transform(myData, arg1, arg2, arg3)

You can also format with the Pipe in the html like this:

{{ myData | myPipe: 'arg1':'arg2':'arg3'... }}

So to make the right date format you can play around with the arguments.

If you want to make global changes you can check:

Gregor Ojstersek
  • 1,369
  • 7
  • 12
0

Use moment.js for date and time, which gives the various date and time functionalities as Follows:

https://momentjs.com/

moment.js have different methods to get the time and date in necessary format.

today = moment()

If you want specific date format,Then you use like

today=moment().format('DD/MM/YYYY')

console.log('Today Date with format e.g 24/07/2018 :',moment().format('DD/MM/YYYY'))

You need to install moment by using following cmd :

Step 1 : npm i moment --save

Step 2 : import * as moment from 'moment'; //in your component code.

Rahul Dapke
  • 345
  • 6
  • 19
  • I have an error. my code: this.wtr.performedOn = moment().format('DD/MM/YYYY'); 'this.wtr.performedOn' show an error: 'type 'string' is not assignable to type 'Date' – Giada SPOT Jul 24 '18 at 11:31
  • Check your variable data type e.g : this.wtr.performedOn . try to make it e.g currentDate:any ,then you use as this.currentDate= moment().format('DD/MM/YYYY'); – Rahul Dapke Jul 24 '18 at 11:43
  • Time remain -2.00 – Giada SPOT Jul 24 '18 at 11:54