时间未通过 moment.js 正确转换为当地时间
Time not getting converted to local time properly via moment.js
11-06-2015 12:44:30
我的日期时间采用上述格式,但它没有转换为当地时间,而是将月份改为 11 月。
var check = moment('@Model.Invoice.InvoiceDate').format('YYYY-MM-DD HH:mm:ss');
var localTime = moment.utc(check).toDate();
localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');
console.log(localTime);
White saving 我正在使用 C# 的 DateTime.UTCNow 函数,在获取数据时我正在使用以下代码。
代码:
var formatDate = new Date('@Model.Invoice.InvoiceDate.ToLocalTime()');
console.log(formatDate);
formatDate = moment.utc(formatDate).toDate();
console.log(formatDate);
var dateTime = moment(formatDate).format('lll');
console.log(dateTime);
正在发生的事情的例子:
var formatDate = new Date('Sat Jun 13 2015 13:00:11 GMT+0530 (India Standard Time)');
console.log(formatDate);
formatDate = moment.utc(formatDate).toDate();
console.log(formatDate);
var dateTime = moment(formatDate).format('lll');
console.log(dateTime);
试试这个
var check = moment('11-06-2015 12:44:30', 'DD-MM-YYYY HH:mm:ss')
.format('YYYY-MM-DD HH:mm:ss');
或者你的情况是这个
var check = moment('@Model.Invoice.InvoiceDate', 'DD-MM-YYYY HH:mm:ss')
.format('YYYY-MM-DD HH:mm:ss');
而不是
var check = moment('@Model.Invoice.InvoiceDate')
.format('YYYY-MM-DD HH:mm:ss');
在为 moments 提供日期字符串时,您需要指定哪一部分是什么。
您可以使用 ToString("s") 暂时打印 UTC ISO 8601 日期,但它会缺少 Z,因此您需要自己添加。
var localTime = moment('@String.concat(Model.Invoice.InvoiceDate.ToString("s"), "Z")').format('lll');
或者在客户端添加 Z :
var localTime = moment('@Model.Invoice.InvoiceDate.ToString("s")' + 'Z').format('lll');
11-06-2015 12:44:30
我的日期时间采用上述格式,但它没有转换为当地时间,而是将月份改为 11 月。
var check = moment('@Model.Invoice.InvoiceDate').format('YYYY-MM-DD HH:mm:ss');
var localTime = moment.utc(check).toDate();
localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');
console.log(localTime);
White saving 我正在使用 C# 的 DateTime.UTCNow 函数,在获取数据时我正在使用以下代码。
代码:
var formatDate = new Date('@Model.Invoice.InvoiceDate.ToLocalTime()');
console.log(formatDate);
formatDate = moment.utc(formatDate).toDate();
console.log(formatDate);
var dateTime = moment(formatDate).format('lll');
console.log(dateTime);
正在发生的事情的例子:
var formatDate = new Date('Sat Jun 13 2015 13:00:11 GMT+0530 (India Standard Time)');
console.log(formatDate);
formatDate = moment.utc(formatDate).toDate();
console.log(formatDate);
var dateTime = moment(formatDate).format('lll');
console.log(dateTime);
试试这个
var check = moment('11-06-2015 12:44:30', 'DD-MM-YYYY HH:mm:ss')
.format('YYYY-MM-DD HH:mm:ss');
或者你的情况是这个
var check = moment('@Model.Invoice.InvoiceDate', 'DD-MM-YYYY HH:mm:ss')
.format('YYYY-MM-DD HH:mm:ss');
而不是
var check = moment('@Model.Invoice.InvoiceDate')
.format('YYYY-MM-DD HH:mm:ss');
在为 moments 提供日期字符串时,您需要指定哪一部分是什么。
您可以使用 ToString("s") 暂时打印 UTC ISO 8601 日期,但它会缺少 Z,因此您需要自己添加。
var localTime = moment('@String.concat(Model.Invoice.InvoiceDate.ToString("s"), "Z")').format('lll');
或者在客户端添加 Z :
var localTime = moment('@Model.Invoice.InvoiceDate.ToString("s")' + 'Z').format('lll');