将javascript日期发送到vb.net日期变量

时间:2022-05-09 20:52:21

I need to pass javascript date value to vb.net function.

我需要将javascript日期值传递给vb.net函数。

Method iam using now: convert javascript date to string store it in hiddenfield retrieve string from hidden field in server code and parse it using date.parse

方法iam现在使用:将javascript日期转换为字符串将其存储在hiddenfield中从服务器代码中的隐藏字段检索字符串并使用date.parse解析它

the trouble is that the Javascript dateformats

麻烦的是Javascript dateformats

toString() - Sat Apr 4 22:19:00 UTC+0530 2009

toString() - Sat Apr 4 22:19:00 UTC + 0530 2009

toDateString() - Sat Apr 4 2009

toDateString() - 2009年4月4日星期六

toLocaleString() - Saturday, April 04, 2009 10:19:00 PM

toLocaleString() - 2009年4月4日星期六,下午10:19:00

doesnt match vb date format. I am getting error that its unparseable.

与vb日期格式不匹配。我收到的错误是它无法解析。

Thanks in advance for the help

在此先感谢您的帮助

5 个解决方案

#1


The problem with using ToLocaleString is that you lose timezone info and its obviously locale specific which means you need to parse it with the right culture.

使用ToLocaleString的问题在于您丢失了时区信息及其明显的区域设置特定,这意味着您需要使用正确的文化解析它。

I was thinking:-

我刚在想:-

DateTime d = DateTime.ParseExact(sInput, "ddd MMM d HH:mm:ss UTCzzzz yyyy" , CultureInfo.InvariantCulture);

DateTime d = DateTime.ParseExact(sInput,“ddd MMM d HH:mm:ss UTCzzzz yyyy”,CultureInfo.InvariantCulture);

But that isn't cross browser compliant (the ECMA spec does not define what toString should actually do).

但这不符合跨浏览器(ECMA规范没有定义toString实际应该做什么)。

However we do know that the value of a Javascript Date object is the number of milliseconds from midnight Jan 1, 1970. Hence you could instead store the .valueOf of a date object in your hidden field. Use Int32.Parse on the string first, create a TimeSpan from the that value and add it to a DateTime of Jan 1, 1970 00:00:00 UTC+0000.

但是我们知道Javascript Date对象的值是从1970年1月1日午夜开始的毫秒数。因此,您可以将日期对象的.valueOf存储在隐藏字段中。首先在字符串上使用Int32.Parse,从该值创建TimeSpan并将其添加到DateTime,1970 00:00:00 UTC + 0000。

int milliseconds = Int32.Parse(inputString);
TimeSpan t = TimeSpan.FromMilliseconds(milliseconds);
DateTime base = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime result = base + t;

#2


Why not instead pass the Javascript date as a string and then convert it to a date type in VB.net.

为什么不将Javascript日期作为字符串传递,然后将其转换为VB.net中的日期类型。

Public Function ConvertJavaScriptDate(ByVal d as String) As Date
  Return Date.Parse(d)
End Function

Another option is to use CType(d,Date). CType is a lexical cast that will try a variety of ways to convert the string value to a Date.

另一种选择是使用CType(d,Date)。 CType是一个词法转换,它会尝试各种方法将字符串值转换为Date。

I'm not terribly familiar with the difference between a JavaScript Date and a VB.Net Date in terms of format, but if you post an example I'm sure we can get a basic conversion going.

我在格式方面并不十分熟悉JavaScript日期和VB.Net日期之间的区别,但如果你发布一个例子,我确信我们可以进行基本的转换。

#3


Since I don't have to worry about culture difference I am going to use toLocaleString().

由于我不必担心文化差异,我将使用toLocaleString()。

toLocaleString() parses fine to a string compatible with Date.Parse().

toLocaleString()解析为与Date.Parse()兼容的字符串。

Anyway thanks for posting your replies.

无论如何,感谢您发布回复。

#4


This just a datetime formating issue can you look this post for more details. How we can resolve the datetime problem shifting the Access DB from production server to live

这只是一个日期时间格式问题,您可以查看此帖子了解更多详情。我们如何解决将Access DB从生产服务器转移到实时的日期时间问题

#5


You can also use DateTime.ParseExact() to tell the VB code exactly what the incoming string should look like.

您还可以使用DateTime.ParseExact()来告诉VB代码确切的传入字符串应该是什么样子。

#1


The problem with using ToLocaleString is that you lose timezone info and its obviously locale specific which means you need to parse it with the right culture.

使用ToLocaleString的问题在于您丢失了时区信息及其明显的区域设置特定,这意味着您需要使用正确的文化解析它。

I was thinking:-

我刚在想:-

DateTime d = DateTime.ParseExact(sInput, "ddd MMM d HH:mm:ss UTCzzzz yyyy" , CultureInfo.InvariantCulture);

DateTime d = DateTime.ParseExact(sInput,“ddd MMM d HH:mm:ss UTCzzzz yyyy”,CultureInfo.InvariantCulture);

But that isn't cross browser compliant (the ECMA spec does not define what toString should actually do).

但这不符合跨浏览器(ECMA规范没有定义toString实际应该做什么)。

However we do know that the value of a Javascript Date object is the number of milliseconds from midnight Jan 1, 1970. Hence you could instead store the .valueOf of a date object in your hidden field. Use Int32.Parse on the string first, create a TimeSpan from the that value and add it to a DateTime of Jan 1, 1970 00:00:00 UTC+0000.

但是我们知道Javascript Date对象的值是从1970年1月1日午夜开始的毫秒数。因此,您可以将日期对象的.valueOf存储在隐藏字段中。首先在字符串上使用Int32.Parse,从该值创建TimeSpan并将其添加到DateTime,1970 00:00:00 UTC + 0000。

int milliseconds = Int32.Parse(inputString);
TimeSpan t = TimeSpan.FromMilliseconds(milliseconds);
DateTime base = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime result = base + t;

#2


Why not instead pass the Javascript date as a string and then convert it to a date type in VB.net.

为什么不将Javascript日期作为字符串传递,然后将其转换为VB.net中的日期类型。

Public Function ConvertJavaScriptDate(ByVal d as String) As Date
  Return Date.Parse(d)
End Function

Another option is to use CType(d,Date). CType is a lexical cast that will try a variety of ways to convert the string value to a Date.

另一种选择是使用CType(d,Date)。 CType是一个词法转换,它会尝试各种方法将字符串值转换为Date。

I'm not terribly familiar with the difference between a JavaScript Date and a VB.Net Date in terms of format, but if you post an example I'm sure we can get a basic conversion going.

我在格式方面并不十分熟悉JavaScript日期和VB.Net日期之间的区别,但如果你发布一个例子,我确信我们可以进行基本的转换。

#3


Since I don't have to worry about culture difference I am going to use toLocaleString().

由于我不必担心文化差异,我将使用toLocaleString()。

toLocaleString() parses fine to a string compatible with Date.Parse().

toLocaleString()解析为与Date.Parse()兼容的字符串。

Anyway thanks for posting your replies.

无论如何,感谢您发布回复。

#4


This just a datetime formating issue can you look this post for more details. How we can resolve the datetime problem shifting the Access DB from production server to live

这只是一个日期时间格式问题,您可以查看此帖子了解更多详情。我们如何解决将Access DB从生产服务器转移到实时的日期时间问题

#5


You can also use DateTime.ParseExact() to tell the VB code exactly what the incoming string should look like.

您还可以使用DateTime.ParseExact()来告诉VB代码确切的传入字符串应该是什么样子。