Problem with my format() functions...

  • Thread starter Thread starter n.net
  • Start date Start date
N

n.net

Due to the much of VBScript inhibition in my early years of website
development, I exhaustively used the following code in my .NET
console applications.

oLog.setStartTime = Format(Now, "MM/dd/yyyy HH:mm:ss")

This worked fine, when ppl used the code in US.

When the same code was migrated to Singapore, the developers started
to complain about the apparent error that occurs, due to the local
machine settings.

For example a date like 04/25/2007 - the Format() Function which
depends on local machine settings is creating problems.

What is the best practise to get rid of the evil? Pls advise.
 
Local setting shouldn't really matter because you are giving your own
formatting. Anyway, try this: DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
 
Yeah just check

Dim sTime as DateTime
StartTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")

When i try on 08/06/2007 it works. But when I change the date to
22/06/2007. (The dates I mentioned is 08-Jun-2007 and 22-Jun-2007)

The exception I get is "Cast from string "06/22/2007 10:27:49" to
type 'Date' is not valid."


Pls advise.
 
n.net said:
Yeah just check

Dim sTime as DateTime
StartTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")

When i try on 08/06/2007 it works. But when I change the date to
22/06/2007. (The dates I mentioned is 08-Jun-2007 and 22-Jun-2007)

The exception I get is "Cast from string "06/22/2007 10:27:49" to
type 'Date' is not valid."

The code that you showed can not produce that error message, as you are
not converting a string to a date in that code.

When you parse the string, either specify a culture that uses that date
format, or use DateTime.ParseExact with the same formatting string as
you used to create the string.
 
Back
Top