Last post Dec 15, 2008 08:51 AM by theDog
Dec 13, 2008 11:17 AM|theDog|LINK
in my ASP.NET website user have to insert date and time in a textbox.
the problem is that 100% of users will be Italian.... but the hosting will be in USA...
so i've to store all date-time data in UTC format in the DB.
How can i use some kind of DateTime.PARSE to tell ASP.NET that the parsing date IS LOCAL, BUT IS ITALIAN and NOT USA?
ASP.net should calculate also daylight saving differences between USA and ITALY... so it's important to say asp.net that the datetime is ITALIAN LOCAL because when i convert it to UTC the value may change!
Please help me. Thanks for any helps!
Dec 13, 2008 11:27 AM|mbanavige|LINK
Give this a read:
for converting time to localtime from utc, i use TimeZoneInfo (available in 3.5)
Dec 13, 2008 12:28 PM|theDog|LINK
Thank you for the reply, but you've not well understood my problem (maybe i can't express my self very well).
in my web.config i've set a COSTANT culture to it-IT
so what's the problem? when a user type into a textbox 15-mar-2008 00:00
I've to convert it into UTC to store it in the DB.
well, as my server is in USA and have USA TimeFuse (UTC-8), when i try to execute
dim dt as datetime=datetime.parse(textbox1.text)
ASP.NET render the UTC date in ITALIAN FORMAT (dd/mm/yyyy hh:mm:ss) but make mistakes in the conversion to UTC
Italy is GMT+1 and on 15-mar IN ITALY there is NO daylight saving time (it start on 30-mar)... so the good result would be UTC 14/03/2008 23:00:00
on the contrary ASP.NET give me 14/03/2008 22:00:00 because in USA daylight saving time started on 9-mar! SO THIS IS an ASP.NET MISTAKE!
Is there any method to make ASP.NET undestand that a parsed date is ITALIAN LOCALIZED?
Dec 13, 2008 01:49 PM|mbanavige|LINK
a culture and a timezone are two completely seperate things. for example, my culture is "en-US". Can you infer my timezone from my culture? no - you cannot.
please look at the timezoneinfo link i provided in my earlier post. You will need to use an Italy based timezoneinfo object to convert your UTC datetimes to italy time. The timezoneinfo object is aware of the daylight saving rules. you would probably
want to use a timezoneinfo object for "Central European Standard Time"
Dec 13, 2008 02:09 PM|theDog|LINK
yes, you are perfectly OK!
i used already the TimeZone object to convert between FROM UTC...
the problem is the PARSING of an ITALIAN TIME **TO** UTC.
it seems i've only 2 solution: declare a parsing as the parsing of an UTC time (AssumeUniversal)....
or i've to declare a parsing as the LOCAL TIME (AssumeLocal).... BUT, AssumeLocal=Assume TimeZone of the Server !
so i'm searching for a solution that can parse a string as an ITALIAN time... so then i can convert it TO UTC with any method...
i'm sorry for my poor english.... maybe you can't understood well my message as i cannot write perfect english....
Dec 13, 2008 02:10 PM|mbanavige|LINK
did you try this:
Dim italy As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time")
Dim userInput As Date = Date.Parse("15-mar-2008 00:00")
Dim utcDate As Date = TimeZoneInfo.ConvertTimeToUtc(userInput, italy)
Dim italyDate As Date = TimeZoneInfo.ConvertTimeFromUtc(utcDate, italy)
Dec 15, 2008 08:51 AM|theDog|LINK
i don't know why in my first trie i've got wronk result with
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, "W. Europe Standard Time", "GMT Standard Time")
anyway now it's ok!