Javascript 时间未通过矩.js正确转换为本地时间

Javascript 时间未通过矩.js正确转换为本地时间,javascript,c#,momentjs,utc,Javascript,C#,Momentjs,Utc,我的datetime具有上述格式,但它没有转换为本地时间,而是将月份转换为11月 11-06-2015 12:44:30 白色保存我使用C#的DateTime.UTCNow函数,在获取数据时,我使用以下代码 代码: 正在发生的情况的示例: var formatDate = new Date('@Model.Invoice.InvoiceDate.ToLocalTime()'); console.log(formatDate);

我的datetime具有上述格式,但它没有转换为本地时间,而是将月份转换为11月

11-06-2015 12:44:30 
白色保存我使用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 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 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 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');

在给时刻一个日期字符串时,您需要指定哪个部分是什么。

您可以使用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')

您可以使用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')

您可以使用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')

您可以使用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')

如果时间格式发生变化怎么办?现在,我的服务器日期时间如下所示:2015年6月12日在保存时,我使用的是c,所以我使用的是DateTime.utcNow如果时间格式发生变化怎么办?现在,我的服务器日期时间如下所示:2015年6月12日在保存时,我使用的是c,所以我使用的是DateTime.utcNow如果时间格式发生变化怎么办?现在,我的服务器日期时间如下所示:2015年6月12日在保存时,我使用的是c,所以我使用的是DateTime.utcNow如果时间格式发生变化怎么办?现在,我的服务器日期时间看起来是这样的:2015年6月12日,在保存时,我使用c#,所以我使用DateTime.UTCNowMy对象是一个DateTime字段,而不是String。这就是为什么有一个ToString(“s”),它将您的DateTime转换为ISO8601日期字符串,.net实现缺少“Z”,所以concat会添加它。我可以这样做吗:Convert.ToDateTime(string.Concat(DateTime.UtcNow.ToString(“s”),“Z”))只要用DateTime对象替换我的DateTime,并将结果放入模板中即可。类似于:string.Concat(Model.Invoice.InvoiceDate.UtcNow.ToString(“s”),“Z”)我的对象是一个DateTime字段,而不是string,这就是为什么会有ToString(“s”),它将您的DateTime转换为ISO8601日期字符串,.net实现缺少“Z”,因此concat会添加它。我可以这样做:Convert.ToDateTime(String.concat(DateTime.UtcNow.ToString(“s”),“Z”))只需用DateTime对象替换我的DateTime,并将结果放入模板中。类似这样的操作:String.concat(Model.Invoice.InvoiceDate.UtcNow.ToString(“s”),“Z”)我的对象是一个DateTime字段,而不是字符串这就是为什么有一个ToString(“s”),它将您的DateTime转换为ISO8601日期字符串,.net实现缺少“Z”,所以concat会添加它。我可以这样做:Convert.ToDateTime(String.concat(DateTime.UtcNow.ToString(“s”),“Z”))只需用DateTime对象替换我的DateTime,并将结果放入模板中。类似于:string.Concat(Model.Invoice.InvoiceDate.UtcNow.ToString(“s”),“Z”)我的对象是一个DateTime字段,而不是string,这就是为什么会有ToString(“s”),它将您的DateTime转换为ISO8601日期字符串,.net实现缺少“Z”,因此concat会添加它。我可以这样做:Convert.ToDateTime(String.concat(DateTime.UtcNow.ToString(“s”),“Z”))只需用DateTime对象替换我的DateTime,并将结果放入模板中。类似这样的操作:String.concat(型号:发票日期:UtcNow ToString(“s”),“Z”)您也可以使用javascript日期对象的本机方法在不使用矩.js的情况下解决此问题。签出此链接-您也可以使用javascript日期对象的本机方法在不使用矩.js的情况下解决此问题。签出此链接-Y您也可以通过使用javascript日期对象的本机方法来解决这个问题,而无需使用moment.js---