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);
白色保存我正在使用 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);
最佳答案
您可以使用 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');
关于javascript - 时间未通过 moment.js 正确转换为本地时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30781802/