javascript - 时间未通过 moment.js 正确转换为本地时间

标签 javascript c# momentjs utc

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/

相关文章:

c# - 使用 @@TRANCOUNT 有用吗?

c# - 使用 Html Agility Pack 从网页中的表中获取值而不使用 "SelectNode'

javascript - 尝试安装 firebase-tools 并收到奇怪的错误

c# - 如何防止对派生类中的静态属性进行设置访问?

javascript - 禁止用户基于浏览器版本访问JS应用

javascript - 迭代日期数组的函数会产生意想不到的结果

javascript - 使用 Angular、Underscore 和 Moment js 按周分组

javascript - 动态 Moment.js 差异

javascript - 带有滚动条的 jQuery ui ReSizable

javascript - 每 5 秒刷新一次 JSON 填充图表的内容