[英]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.