shizil
shizil

Reputation: 37

Angular Date type

From my angular interface date value coming like this.

Sat Dec 29 2018 08:42:06 GMT+0400 (Gulf Standard Time)

but after receiving data from api JSON result look like this. how to make it same type?

 2018-12-27T13:37:00.83

Typescript

export interface header{
    vr_date : Date  
}

Asp API class

 public class header
 { 
      public Nullable<System.DateTime> vr_date { get; set; }
 }

Upvotes: 1

Views: 2898

Answers (1)

Prashant Pimpale
Prashant Pimpale

Reputation: 10717

You can use Angular's inbuilt DatePipe. So all you need to do is:

In Component TS:

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

@Component({
 // other stuff
  providers:[DatePipe]
})

constructor(public datePipe : DatePipe){

}

To use it:

var format = "yyyy-MM-dd HH:mm:ss"; // this could be `yyyy-MM-dd` or `MM/dd/yyyy`
this.datePipe.transform(your_date_variable, format);

EDIT:

If you want to convert a date into a GMT then use your_date.toUTCString()

then use:

var isoDate = new Date('2018-12-27T13:37:00.83');
var UTCDate = isoDate.toUTCString();

Which returns:

Thu, 27 Dec 2018 08:07:00 GMT

WORKING DEMO

Upvotes: 4

Related Questions