MomentJs: How to convert string to date in Typescript?

Im implements Ag Gird in Angular 2. I have a date column and date sorting, and the Ag grid expects the date type in format only. So I had to send it as a date for rendering. But my input is a string. I have a dateString in the format 2017-01-19. I do

let myDate:Date = moment(dateString).toDate();

But this givimg takes me out like Thu January 19, 2017 00:00 Indian Standard Time. I tried it too

let myDate:Date = moment(dateString).format("DD/MM/YYYY");

This gives me an error, the string is not assigned a Date.

Tried it too

let myDate:Date = moment(dateString,"DD/MM/YYYY");

But no luck. I want the type of output as date and in the form 01/19/2017

Please suggest

+4
source share
3 answers

Try the following:

let myDate: Date = moment (dateString, "YYYY-MM-DD" ). format ( "DD-MM-YYYY" );

, moment(), , , , . .format().

+6
moment().toDate();

Date

Moment.js

+1

. date javascript. new Date("2017.02.28"), .

{
  colId: "date",
  headerName: "Date",
  field: "date",
  cellRendererFramework: GridDateComponent,
}

, , date, JSDate ..

import { Component } from '@angular/core';
import { AgRendererComponent } from 'ag-grid-ng2/main';

@Component({
    selector: 'date-cell',
    template: `{{params.value | date:'d MMM'}}`
})
export class GridDateComponent implements AgRendererComponent {
    public params: any;

    agInit(params: any): void {
        this.params = params;
    }
}
0

Source: https://habr.com/ru/post/1671057/


All Articles