How to convert Dot net sql date / Date string to Local UTC of moment in angular 6

How to convert Dot net sql date / Date string to Local UTC of moment in angular 6


1). For Use In HTML Create Pipe :     

In Angular 6  first we Have create a pipe With Help of Command 

"ng g p convertSqltoUTC "

and Now You Need to Copy paste follow Code into your pipe

import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
  name: 'convertUTCSqlDate'
})
export class ConvertUTCSqlDatePipe implements PipeTransform {
  transform(date: any, format = 'DD/MM/YYYY HH:mm:ss') {
    if (date) {
      let localText;
      if (date instanceof moment) {
        localText = moment(moment.utc(date._i, format).toDate()).format(format);
      } else {
        localText = moment(moment.utc(date, format).toDate()).format(format);
      }
      return localText;
    } else {
      return '';
    }
  }
}


Now you can User This pipe in your html to convert Date string to Utc in any format 

<h5>{{element.CreateDatre | convertUTCSqlDate }} </h5>

_________________________________________________________________


2). For Componet.ts File Use Service Like:

for Create a Service Run Below Command 

"ng g s ConvertUTCSqlDateService "

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class ConvertUTCSqlDateService {
 ConvertUTCSqlDate(date: any, format = 'DD/MM/YYYY HH:mm:ss') {
    if (date) {
      let localText;
      if (date instanceof moment) {
        localText = moment(moment.utc(date._i, format).toDate()).format(format);
      } else {
        localText = moment(moment.utc(date, format).toDate()).format(format);
      }
      return localText;    } else {
      return '';
    }
  }
}
Now You Need To Inject Service and call method return local Utc Date



1 comment:

  1. Thank you so much for sharing such an informative article. Visit my blog to get more info about the Minnesota web design.

    ReplyDelete

Thank You For Your Great Contribution

Featured Post

How to remove multiple item from FormArray

 How to remove multiple item from FormArray Call Method at any component :- this.removeItems(formArray, formcontrolCountryID, 2);  Meth...

Popular Posts