Javascript MomentJs:如何在 Typescript 中将字符串转换为日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42513492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:12:44  来源:igfitidea点击:

MomentJs: How to convert string to date in Typescript?

javascriptangulartypescriptmomentjsag-grid

提问by Protagonist

Im implementing Ag Gird in Angular 2. I have a date column and sorting of dates and Ag grid expects type of date in the format only. Thus I had to send it in the form of date for rendering. But my input is string. I have my dateString in the format 2017-01-19. I'm doing

我在 Angular 2 中实现 Ag 网格。我有一个日期列和日期排序,而 Ag 网格只需要格式中的日期类型。因此,我不得不以日期的形式发送它以进行渲染。但我的输入是字符串。我的 dateString 格式为 2017-01-19。我正在做

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

But it's givimg me output as Thu Jan 19 2017 00:00 Indian Standard Time. I tried like this too

但它给我输出为 2017 年 1 月 19 日星期四 00:00 印度标准时间。我也试过这样

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

This gives me error- string is not assignable to Date.

这给了我错误字符串不能分配给日期。

Tried like this too

也这样试过

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

But no luck. What I want is, type of the output as date and in the form 19-01-2017

但没有运气。我想要的是,输出类型为日期和形式 19-01-2017

Please suggest

请建议

回答by Wissam

Try this:

尝试这个:

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

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

Basically, when you call moment(), you should specify the format your current date is in, notthe output format that you want. The output format should be specified when calling .format().

基本上,当您调用 时moment(),您应该指定当前日期的格式,而不是您想要的输出格式。调用时应指定输出格式.format()

回答by Alx

moment().toDate();

gives a Date object

给出一个 Date 对象

got the answer from Moment.js transform to date object

Moment.js 转换为日期对象得到答案

回答by Christopher Moore

You should look to utilise a column renderer frameworkin your column definitions. Where the field dateshould be a javascript date. You can generate this from a string via new Date("2017.02.28"), there is no need here to use moment.

您应该在列定义中使用列渲染器框架。该字段date应为 javascript 日期的位置。您可以通过从字符串生成它new Date("2017.02.28"),这里不需要使用 moment。

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

This component displays the date formatted how you want utilising the datepipe, whilst preserving the original JSDate for sorting etc.

此组件显示您希望如何使用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;
    }
}