Timezone conversion | | |
I have an events table which stores the time of each event - the time and
assoicated timezone. Is there a way of converting this time into GMT (with
support for DST).. some sort of function which accepts a time and timezone
as parameters and returns the time in GMT? | | | | re: Timezone conversion
Microsoft recommends using UTC time (universal Time Coordinate)...similar to
the GMT concept. If you use that...
DateTime.ToLocalTime()
DateTime.ToUniversalTime()
If you want GMT, you'll have to write your own utility class.
-Jason
"Bijoy Naick" <b_naick@yahoo.ca> wrote in message
news:eqpWn0F5EHA.3828@TK2MSFTNGP09.phx.gbl...[color=blue]
>I have an events table which stores the time of each event - the time and
> assoicated timezone. Is there a way of converting this time into GMT (with
> support for DST).. some sort of function which accepts a time and timezone
> as parameters and returns the time in GMT?
>
>[/color] | | | | re: Timezone conversion
these functions wont allow me to convert a time in Pacific time to UTC..
will they?
"Jason Penniman" <jpenniman@actcci.com> wrote in message
news:%235$BT%23F5EHA.2664@TK2MSFTNGP10.phx.gbl...[color=blue]
> Microsoft recommends using UTC time (universal Time Coordinate)...similar[/color]
to[color=blue]
> the GMT concept. If you use that...
>
> DateTime.ToLocalTime()
> DateTime.ToUniversalTime()
>
> If you want GMT, you'll have to write your own utility class.
>
> -Jason
>
> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
> news:eqpWn0F5EHA.3828@TK2MSFTNGP09.phx.gbl...[color=green]
> >I have an events table which stores the time of each event - the time and
> > assoicated timezone. Is there a way of converting this time into GMT[/color][/color]
(with[color=blue][color=green]
> > support for DST).. some sort of function which accepts a time and[/color][/color]
timezone[color=blue][color=green]
> > as parameters and returns the time in GMT?
> >
> >[/color]
>
>[/color] | | | | re: Timezone conversion
Yes they will. Let's say you need to convert the current time.
DateTime currentTime = DateTime.Now;
DateTime utcTime = currentTime.ToUniveralTime();
or the short version: DateTime utcTime = DateTime.Now.ToUniversalTime();
If you need to use a date other than "now":
DateTime myDate = DateTime.Parse("7/4/2004 13:00:00");
DateTime utcTime = myDate.ToUniversalTime();
the short version: DateTime utcTime = DateTime.Parse("7/4/2004
13:00:00").ToUniversalTime();
To convert back:
DateTime myDate = utcTime.ToLocalTime();
Of course, this example assumes you are using the timezone of the machine
the application lives on. If you're on the east cost and need to convert to
pacific, you can use the Calendar Object to set the locale and pull your
dates/times from that. Though, if you need that flexability, and are going
to bu reusing that code, I would take the custom utility class approach.
"Bijoy Naick" <b_naick@yahoo.ca> wrote in message
news:uVKXcCG5EHA.1120@TK2MSFTNGP11.phx.gbl...[color=blue]
> these functions wont allow me to convert a time in Pacific time to UTC..
> will they?
>
> "Jason Penniman" <jpenniman@actcci.com> wrote in message
> news:%235$BT%23F5EHA.2664@TK2MSFTNGP10.phx.gbl...[color=green]
>> Microsoft recommends using UTC time (universal Time Coordinate)...similar[/color]
> to[color=green]
>> the GMT concept. If you use that...
>>
>> DateTime.ToLocalTime()
>> DateTime.ToUniversalTime()
>>
>> If you want GMT, you'll have to write your own utility class.
>>
>> -Jason
>>
>> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
>> news:eqpWn0F5EHA.3828@TK2MSFTNGP09.phx.gbl...[color=darkred]
>> >I have an events table which stores the time of each event - the time
>> >and
>> > assoicated timezone. Is there a way of converting this time into GMT[/color][/color]
> (with[color=green][color=darkred]
>> > support for DST).. some sort of function which accepts a time and[/color][/color]
> timezone[color=green][color=darkred]
>> > as parameters and returns the time in GMT?
>> >
>> >[/color]
>>
>>[/color]
>
>[/color] | | | | re: Timezone conversion
do u have any examples of the calendar object?
"Jason Penniman" <jpenniman@actcci.com> wrote in message
news:OvaWnbG5EHA.3648@TK2MSFTNGP11.phx.gbl...[color=blue]
> Yes they will. Let's say you need to convert the current time.
>
> DateTime currentTime = DateTime.Now;
> DateTime utcTime = currentTime.ToUniveralTime();
>
> or the short version: DateTime utcTime = DateTime.Now.ToUniversalTime();
>
> If you need to use a date other than "now":
>
> DateTime myDate = DateTime.Parse("7/4/2004 13:00:00");
> DateTime utcTime = myDate.ToUniversalTime();
>
> the short version: DateTime utcTime = DateTime.Parse("7/4/2004
> 13:00:00").ToUniversalTime();
>
> To convert back:
>
> DateTime myDate = utcTime.ToLocalTime();
>
> Of course, this example assumes you are using the timezone of the machine
> the application lives on. If you're on the east cost and need to convert[/color]
to[color=blue]
> pacific, you can use the Calendar Object to set the locale and pull your
> dates/times from that. Though, if you need that flexability, and are[/color]
going[color=blue]
> to bu reusing that code, I would take the custom utility class approach.
>
>
> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
> news:uVKXcCG5EHA.1120@TK2MSFTNGP11.phx.gbl...[color=green]
> > these functions wont allow me to convert a time in Pacific time to UTC..
> > will they?
> >
> > "Jason Penniman" <jpenniman@actcci.com> wrote in message
> > news:%235$BT%23F5EHA.2664@TK2MSFTNGP10.phx.gbl...[color=darkred]
> >> Microsoft recommends using UTC time (universal Time[/color][/color][/color]
Coordinate)...similar[color=blue][color=green]
> > to[color=darkred]
> >> the GMT concept. If you use that...
> >>
> >> DateTime.ToLocalTime()
> >> DateTime.ToUniversalTime()
> >>
> >> If you want GMT, you'll have to write your own utility class.
> >>
> >> -Jason
> >>
> >> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
> >> news:eqpWn0F5EHA.3828@TK2MSFTNGP09.phx.gbl...
> >> >I have an events table which stores the time of each event - the time
> >> >and
> >> > assoicated timezone. Is there a way of converting this time into GMT[/color]
> > (with[color=darkred]
> >> > support for DST).. some sort of function which accepts a time and[/color]
> > timezone[color=darkred]
> >> > as parameters and returns the time in GMT?
> >> >
> >> >
> >>
> >>[/color]
> >
> >[/color]
>
>[/color] | | | | re: Timezone conversion
Sorry... in Java mode these days... The Calendar class in .Net doesn't allow
you to set the timezone. There is a TimeZone class, but it also uses the
timezone of the current computer.
Looks like, if you want that flexability, you're going to have to write you
own. You could Inherit the TimeZone class (DateTime is a structure and not
inheritable) and overload the ToLocalTime() method. That way, your new
class has all the other functionality you need to work with TimeZones and
UTC time.
Something like....
public DateTime ToLocalTime(DateTime dt, string tz) {
if (tz.ToLower().Equals("pst")) {
return dt.AddHours(-8);
}
}
Obviously, you'll have to get a litte more fancy to get around the daylight
savings time problem. You'll have to test it to get the math right, but you
can use the TimeZone.IsDaylightSavingTime method coupled with the
DaylightTime class to check if the date is within daylight savings time.
"Bijoy Naick" <b_naick@yahoo.ca> wrote in message
news:eBu29dG5EHA.1596@tk2msftngp13.phx.gbl...[color=blue]
> do u have any examples of the calendar object?
>
> "Jason Penniman" <jpenniman@actcci.com> wrote in message
> news:OvaWnbG5EHA.3648@TK2MSFTNGP11.phx.gbl...[color=green]
>> Yes they will. Let's say you need to convert the current time.
>>
>> DateTime currentTime = DateTime.Now;
>> DateTime utcTime = currentTime.ToUniveralTime();
>>
>> or the short version: DateTime utcTime = DateTime.Now.ToUniversalTime();
>>
>> If you need to use a date other than "now":
>>
>> DateTime myDate = DateTime.Parse("7/4/2004 13:00:00");
>> DateTime utcTime = myDate.ToUniversalTime();
>>
>> the short version: DateTime utcTime = DateTime.Parse("7/4/2004
>> 13:00:00").ToUniversalTime();
>>
>> To convert back:
>>
>> DateTime myDate = utcTime.ToLocalTime();
>>
>> Of course, this example assumes you are using the timezone of the machine
>> the application lives on. If you're on the east cost and need to convert[/color]
> to[color=green]
>> pacific, you can use the Calendar Object to set the locale and pull your
>> dates/times from that. Though, if you need that flexability, and are[/color]
> going[color=green]
>> to bu reusing that code, I would take the custom utility class approach.
>>
>>
>> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
>> news:uVKXcCG5EHA.1120@TK2MSFTNGP11.phx.gbl...[color=darkred]
>> > these functions wont allow me to convert a time in Pacific time to
>> > UTC..
>> > will they?
>> >
>> > "Jason Penniman" <jpenniman@actcci.com> wrote in message
>> > news:%235$BT%23F5EHA.2664@TK2MSFTNGP10.phx.gbl...
>> >> Microsoft recommends using UTC time (universal Time[/color][/color]
> Coordinate)...similar[color=green][color=darkred]
>> > to
>> >> the GMT concept. If you use that...
>> >>
>> >> DateTime.ToLocalTime()
>> >> DateTime.ToUniversalTime()
>> >>
>> >> If you want GMT, you'll have to write your own utility class.
>> >>
>> >> -Jason
>> >>
>> >> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
>> >> news:eqpWn0F5EHA.3828@TK2MSFTNGP09.phx.gbl...
>> >> >I have an events table which stores the time of each event - the time
>> >> >and
>> >> > assoicated timezone. Is there a way of converting this time into GMT
>> > (with
>> >> > support for DST).. some sort of function which accepts a time and
>> > timezone
>> >> > as parameters and returns the time in GMT?
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >[/color]
>>
>>[/color]
>
>[/color] | | | | re: Timezone conversion
I've been using the ToUniversalTime object with great success until we moved
the clocks forward for daylight savins time. Now, ToUniversalTime shows UTC
+ 1 hour. How can I compensate for this automatically?
Brett
"Jason Penniman" wrote:
[color=blue]
> Sorry... in Java mode these days... The Calendar class in .Net doesn't allow
> you to set the timezone. There is a TimeZone class, but it also uses the
> timezone of the current computer.
>
> Looks like, if you want that flexability, you're going to have to write you
> own. You could Inherit the TimeZone class (DateTime is a structure and not
> inheritable) and overload the ToLocalTime() method. That way, your new
> class has all the other functionality you need to work with TimeZones and
> UTC time.
>
> Something like....
>
> public DateTime ToLocalTime(DateTime dt, string tz) {
> if (tz.ToLower().Equals("pst")) {
> return dt.AddHours(-8);
> }
> }
>
> Obviously, you'll have to get a litte more fancy to get around the daylight
> savings time problem. You'll have to test it to get the math right, but you
> can use the TimeZone.IsDaylightSavingTime method coupled with the
> DaylightTime class to check if the date is within daylight savings time.
>
>
>
>
>
> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
> news:eBu29dG5EHA.1596@tk2msftngp13.phx.gbl...[color=green]
> > do u have any examples of the calendar object?
> >
> > "Jason Penniman" <jpenniman@actcci.com> wrote in message
> > news:OvaWnbG5EHA.3648@TK2MSFTNGP11.phx.gbl...[color=darkred]
> >> Yes they will. Let's say you need to convert the current time.
> >>
> >> DateTime currentTime = DateTime.Now;
> >> DateTime utcTime = currentTime.ToUniveralTime();
> >>
> >> or the short version: DateTime utcTime = DateTime.Now.ToUniversalTime();
> >>
> >> If you need to use a date other than "now":
> >>
> >> DateTime myDate = DateTime.Parse("7/4/2004 13:00:00");
> >> DateTime utcTime = myDate.ToUniversalTime();
> >>
> >> the short version: DateTime utcTime = DateTime.Parse("7/4/2004
> >> 13:00:00").ToUniversalTime();
> >>
> >> To convert back:
> >>
> >> DateTime myDate = utcTime.ToLocalTime();
> >>
> >> Of course, this example assumes you are using the timezone of the machine
> >> the application lives on. If you're on the east cost and need to convert[/color]
> > to[color=darkred]
> >> pacific, you can use the Calendar Object to set the locale and pull your
> >> dates/times from that. Though, if you need that flexability, and are[/color]
> > going[color=darkred]
> >> to bu reusing that code, I would take the custom utility class approach.
> >>
> >>
> >> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
> >> news:uVKXcCG5EHA.1120@TK2MSFTNGP11.phx.gbl...
> >> > these functions wont allow me to convert a time in Pacific time to
> >> > UTC..
> >> > will they?
> >> >
> >> > "Jason Penniman" <jpenniman@actcci.com> wrote in message
> >> > news:%235$BT%23F5EHA.2664@TK2MSFTNGP10.phx.gbl...
> >> >> Microsoft recommends using UTC time (universal Time[/color]
> > Coordinate)...similar[color=darkred]
> >> > to
> >> >> the GMT concept. If you use that...
> >> >>
> >> >> DateTime.ToLocalTime()
> >> >> DateTime.ToUniversalTime()
> >> >>
> >> >> If you want GMT, you'll have to write your own utility class.
> >> >>
> >> >> -Jason
> >> >>
> >> >> "Bijoy Naick" <b_naick@yahoo.ca> wrote in message
> >> >> news:eqpWn0F5EHA.3828@TK2MSFTNGP09.phx.gbl...
> >> >> >I have an events table which stores the time of each event - the time
> >> >> >and
> >> >> > assoicated timezone. Is there a way of converting this time into GMT
> >> > (with
> >> >> > support for DST).. some sort of function which accepts a time and
> >> > timezone
> >> >> > as parameters and returns the time in GMT?
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>[/color]
> >
> >[/color]
>
>
>[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|