problem with date format

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

Hi,
I store the date selected in a datetimepicker into a sqlserver table in
a SmallDateTime field.
To be compatible with the date format of my sql server, I convert the
date as following:

Dim d As Date
Dim dInDate As Date

Dim ciOriginal As cultureinfo
Dim ciDatabase As cultureinfo

ciOriginal = Thread.CurrentThread.CurrentCulture
ciDatabase = CultureInfo.CreateSpecificCulture("en-US")

Thread.CurrentThread.CurrentCulture = ciDatabase

dInDate = Date.Parse(Data("ExpiryDate").ToString)

d = Date.Parse(Data("ExpiryDate").ToString)

It works fine. Issue is : how to get the sqlserver date format as I
don't want to hardcode en-US as I do above.

Thanks!
 
Sam said:
Hi,
I store the date selected in a datetimepicker into a sqlserver table in
a SmallDateTime field.
To be compatible with the date format of my sql server, I convert the
date as following:

Dim d As Date
Dim dInDate As Date

Dim ciOriginal As cultureinfo
Dim ciDatabase As cultureinfo

ciOriginal = Thread.CurrentThread.CurrentCulture
ciDatabase = CultureInfo.CreateSpecificCulture("en-US")

Thread.CurrentThread.CurrentCulture = ciDatabase

dInDate = Date.Parse(Data("ExpiryDate").ToString)

Hang on. Data("ExpiryDate") comes from SQL, and it's a date, so why
convert it to a string (creating culture issues)? A SqlDateTime can be
directly converted to a Date - this avoids all culture issues.
 
Sam,

Both the DateTime and the SmallDateTime are culture independend.

From MSDN about SmallDateTime
The first 2 bytes store the number of days after January 1, 1900. The other
2 bytes store the number of minutes since midnight. Dates range from January
1, 1900, through June 6, 2079, with accuracy to the minute.

Therefore the value from your datetimepicker is already in the right
datetime format.

ciDatabase = datetimepicker.value should be enough.

I hope this helps,

Cor
 
Hi guys,
Thanks a lot for your replies.
In fact, I had trouble with that because I was doing
datetimepicker.Text instead of datetimepicker.value as Cor suggested.

It works now. ;-)
Thank you.
 
Back
Top