Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with date format

Sam
Guest
 
Posts: n/a
#1: Dec 13 '05
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!


Larry Lard
Guest
 
Posts: n/a
#2: Dec 13 '05

re: problem with date format



Sam wrote:[color=blue]
> 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)[/color]

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.

--
Larry Lard
Replies to group please

Cor Ligthert [MVP]
Guest
 
Posts: n/a
#3: Dec 13 '05

re: problem with date format


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

"Sam" <replyfromforum@hotmail.co.uk> schreef in bericht
news:1134481647.384014.14890@z14g2000cwz.googlegro ups.com...[color=blue]
> 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!
>[/color]


Sam
Guest
 
Posts: n/a
#4: Dec 13 '05

re: problem with date format


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.

Closed Thread