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



No comments:

Post a comment

Thank You For Your Great Contribution

Featured Post

Asp.net And HTML Css And Web Development: How to convert Dot net sql date / Date string to L...

Asp.net And HTML Css And Web Development: How to convert Dot net sql date / Date string to L... : How to convert Dot net sql date / Date str...

Popular Posts