问题描述
我一直在读,如果要将JavaScript日期转换为C#日期,您应该使用 getTime()
,然后将该结果添加到C# DateTime
。
I have been reading that if you want to convert from JavaScript dates to C# dates you should use getTime()
and then add that result to a C# DateTime
.
假设我有这个JavaScript时间:
Suppose I have this JavaScript time:
Date {Tue Jul 12 2011 16:00:00 GMT-0700 (Pacific Daylight Time)}
它呈现为 1310522400000
毫秒
var a = new DateTime(1970, 01, 01).AddMilliseconds(1310522400000);
// result
7/13/2011 2:00:00 AM
所以这是错误的。我不知道我需要做什么。
So this is wrong. I am not sure what I need to do.
推荐答案
首先使用JavaScript中的以下函数创建所需格式的字符串
First create a string in your required format using the following functions in JavaScript
var date = new Date();
var day = date.getDate(); // yields date
var month = date.getMonth() + 1; // yields month (add one as '.getMonth()' is zero indexed)
var year = date.getFullYear(); // yields year
var hour = date.getHours(); // yields hours
var minute = date.getMinutes(); // yields minutes
var second = date.getSeconds(); // yields seconds
// After this construct a string with the above results as below
var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second;
> DateTime.ParseExact()在codebehind中将此字符串转换为 DateTime
如下,
Pass this string to codebehind function and accept it as a string parameter.Use the DateTime.ParseExact()
in codebehind to convert this string to DateTime
as follows,
DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
希望这有帮助...
这篇关于如何将Javascript datetime转换为C#datetime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!