gpt4 book ai didi

vb.Net时间跨度计算

转载 作者:行者123 更新时间:2023-12-01 12:48:19 24 4
gpt4 key购买 nike

我可以使用给定的代码在 vb.net 中计算时间跨度

    Dim dtStartDate As Date = "17/12/2032"
Dim iNumberOfDays As Integer
Dim tsTimeSpan As TimeSpan
tsTimeSpan = Now.Subtract(dtStartDate)
iNumberOfDays = tsTimeSpan.Days 'Where iNumberOfDays is the result of the timespan

当我的电脑时间格式是

dd-MMM-yy (which is found in Control Panel> Date and Time> Change date and Time> Change Calendar Settings> Date Formats.

如果我将计算机日期格式更改为类似MM/dd/yy 的格式,我的应用程序会在行中给出错误

Dim dtStartDate As Date = "17/12/2032"

错误是“从字符串“17/12/2032”到类型日期的转换无效。

我需要计算所有日期格式的时间跨度。如何解决问题?

最佳答案

您可以改用日期文字,它们与文化无关。参见 this article on MSDN :

You must enclose a Date literal within number signs (# #). You must specify the date value in the format M/d/yyyy... This requirement is independent of your locale and your computer's date and time format settings.

所以你可以这样写:

Dim dtStartDate As Date = #12/17/2032#

关于vb.Net时间跨度计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com