简体   繁体   English

Angular:将 Object 转换为日期和时刻

[英]Angular : Convert Object into Date and Moment

I am receiving a form value for date in Kendo Datepicker.我在 Kendo Datepicker 中收到日期的表单值。 It comes in as an object.它以 object 的形式出现。

图片

1) How do I convert the following to date? 1)如何将以下内容转换为日期?

The following in Debugger still left it as Object调试器中的以下内容仍然保留为 Object

new Date(this.editHeaderAddressForm.value.seasonalEnd);

2) Additionally, how can I convert it to Moment? 2)此外,我怎样才能将其转换为 Moment?

Use Date.parse method使用 Date.parse 方法

const epochValue = Date.parse(this.editHeaderAddressForm.value.seasonalEnd);

This will return date in EPOCH this you can directly pass in to Moment function as well.这将在 EPOCH 中返回日期,您也可以直接将其传递给 Moment function。

moment.unix(epochValue)

You can get date, and moment with short and simple code.您可以使用简短的代码获取日期和时刻。

var full_time = new Date(this.editHeaderAddressForm.value.seasonalEnd);

var date = full_time.toLocaleDateString();
var moment = full_time.toLocaleTimeString();

console.log(date);
console.log(moment);

The Date.parse() method parses a string representation of a date, and returns the number of milliseconds Date.parse() 方法解析日期的字符串表示,并返回毫秒数

var javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');

var newDate = Date('December 17, 1995 03:24:00');

console.log(javaScriptRelease);


> // expected output: 818035920000

console.log(newDate);

> // expected output: Sun Dec 17 1995 03:24:00 GMT...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用矩将日期字符串转换为日期对象 - Convert date string to date object using moment 如何将时刻日期转换为字符串并删除时刻对象 - How to convert moment date to a string and remove the moment object 将无效日期转换为矩对象的最简单方法 - Simplest way to Convert to invalid date to a moment object Angular 和 moment.js:我应该将从 json 字符串中获取的字段转换为 Date 或 moment.Moment 吗? - Angular and moment.js : should I convert fields taken from json string to Date or moment.Moment? 错误地将日期时间转换为角度的时刻库 - mistake to convert date time in angular with moment library in angular 如何使用 moment 将本地化的日期字符串转换为日期对象 - How to convert localized date string to date object using moment 我们如何将时刻日期转换为日期对象? - How do we convert moment date in to a date object? 角度材料日期选择器返回 Moment 对象而不是 Date - Angular material date picker return Moment object instead of Date 使用 Moment Js 将日期列表转换为 Angular 中的日期范围 - Convert a list of dates to date range in Angular Using Moment Js 将没有时区的日期字符串转换为特定时区时刻 object - Convert date string without timezone to specific timezone moment object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM