[英]JavaScript GetDate not working across regions
I have some JS code which takes in the client's date/time and passes it to the server like so: 我有一些JS代码,它接受客户端的日期/时间,并将其传递给服务器,如下所示:
function SetPostbackValues() {
//Function that gets the client machine datetime and stores it in a hidden field
// so it may be used in code behind.
var date = new Date();
var day = date.getDate(); // yields day
if (day < 10)
day = '0' + day;
var month = date.getMonth() + 1; // yields month
if (month < 10)
month = '0' + month;
var year = date.getFullYear(); // yields year
var hour = date.getHours(); // yields hours
if (hour < 10)
hour = '0' + hour;
var minute = date.getMinutes(); // yields minutes
if (minute < 10)
minute = '0' + minute;
var second = date.getSeconds(); // yields seconds
if (second < 10)
second = '0' + second;
var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second;
var hiddenControl = '<%= hfDateTime.ClientID %>';
document.getElementById(hiddenControl).value = time;
}
My problem is that the code works fine when the client time zone settings are set to a UK standard, dd/MM/yyyy but when a US client connects, the conversion to DateTime throws an error saying it is not of the right format. 我的问题是,当客户端时区设置设置为英国标准dd / MM / yyyy时,代码运行良好,但是当美国客户端连接时,转换为DateTime会引发错误,指出其格式不正确。
Because I am getting each month, day, year separately and combining them, I do not understand why it does not work across different zone settings. 因为我分别获取每个月,日,年并将它们组合在一起,所以我不明白为什么它不能在不同的区域设置中起作用。
The error occurs when trying to insert datetime into SQL: 尝试将日期时间插入SQL时发生错误:
using (SqlCommand cmd = new SqlCommand("Remove", con))
{
string test = (this.Master.FindControl("hfDateTime") as HiddenField).Value;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@RemovalDate", SqlDbType.DateTime).Value = (this.Master.FindControl("hfDateTime") as HiddenField).Value; //Get the client's datetime.
con.Open();
insertedRecordID = (int)cmd.ExecuteScalar();
}
The error is {"Failed to convert parameter value from a String to a DateTime."} 错误是{“无法将参数值从字符串转换为DateTime。”}
The value of test is: "19/02/2016 10:55:45" which does not look wrong to me. 测试的值是:“ 19/02/2016 10:55:45”这对我来说似乎没有错。
I think you should use this: 我认为您应该使用此:
function SetPostbackValues() {
var date = new Date();
var hiddenControl = '<%= hfDateTime.ClientID %>';
document.getElementById(hiddenControl).value = date.toISOString();
}
And change codebehind to 并将代码更改为
cmd.Parameters.Add("@RemovalDate", SqlDbType.DateTime).Value = DateTime.Parse((this.Master.FindControl("hfDateTime") as HiddenField).Value);
It might be easier to use (new Date()).getTime()
avoiding the different datetime formats. 使用(new Date()).getTime()
避免使用不同的日期时间格式可能会更容易。 Note that C# DateTime
has a different base and scale for its constructor than Javascript. 请注意,C# DateTime
的构造函数的基准和标度与Javascript不同。
Depending on whether you need UTC or local time, subtract datevalue.getTimezoneOffset() * 60000
. 根据您是否需要UTC或本地时间,减去datevalue.getTimezoneOffset() * 60000
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.