I can calculate the timespan in vb.net using the given code
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
It works perfectly when my computers time format is
dd-MMM-yy (which is found in Control Panel> Date and Time> Change date and Time> Change Calendar Settings> Date Formats.
If I change the computer Date Format to something like this MM/dd/yy, my application gives error in the line
Dim dtStartDate As Date = "17/12/2032"
The error is "Conversion from string "17/12/2032" to type date is not valid.
I need to calculate the timespan in all date format. How to solve the problem?
You can use date literals instead, they are culture independent. See 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.
So you can write like this:
Dim dtStartDate As Date = #12/17/2032#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With