473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

datetime with offset > local and utc

Hi,

I have a datetime+offset as a string and want to get two datetimes
from it; the local and UTC. So far I can only do it by splitting on
the + or - of the offset and creating a new date with and without the
offset. Is there a cleaner way, i.e., using System.DateTime etc. and
not resorting to string splitting? I had read about DateTimeOffset
which seems ideal but this is an ASP.Net 2.0 app and that seems to be
only in Framework 3.5 :(

Chandy

Aug 1 '07 #1
4 3158
On Aug 1, 7:40 am, cha...@totalise.co.uk wrote:
Hi,

I have a datetime+offset as a string and want to get two datetimes
from it; the local and UTC. So far I can only do it by splitting on
the + or - of the offset and creating a new date with and without the
offset. Is there a cleaner way, i.e., using System.DateTime etc. and
not resorting to string splitting? I had read about DateTimeOffset
which seems ideal but this is an ASP.Net 2.0 app and that seems to be
only in Framework 3.5 :(
If the format of your date string is predictable, you can do thus:

string s = "2007-02-02 00:00 -8";
DateTime d = DateTime.ParseExact(s, "yyyy-dd-MM hh:mm z",
CultureInfo.CurrentCulture); // 'z' is the time zone offset specifier
Console.WriteLine(d.Kind); // prints "Local"
Console.WriteLine(d); // prints "2/2/2007 12:00:00 AM"
Console.WriteLine(d.ToUniversalTime()); // prints "2/1/2007 8:00:00
AM"

Look up "Custom DateTime Format Strings" in MSDN for more info.

Michael

Aug 1 '07 #2
On Aug 1, 8:33 am, "mpetro...@gmail.com" <mpetro...@gmail.comwrote:
Console.WriteLine(d.Kind); // prints "Local"
Console.WriteLine(d); // prints "2/2/2007 12:00:00 AM"
Console.WriteLine(d.ToUniversalTime()); // prints "2/1/2007 8:00:00
AM"
Minor error: the output is actually:

Local
2/2/2007 12:00:00 AM
2/2/2007 8:00:00 AM

....as it should be.

Aug 1 '07 #3
On Aug 1, 8:36 am, "mpetro...@gmail.com" <mpetro...@gmail.comwrote:
On Aug 1, 8:33 am, "mpetro...@gmail.com" <mpetro...@gmail.comwrote:
Console.WriteLine(d.Kind); // prints "Local"
Console.WriteLine(d); // prints "2/2/2007 12:00:00 AM"
Console.WriteLine(d.ToUniversalTime()); // prints "2/1/2007 8:00:00
AM"

Minor error: the output is actually:

Local
2/2/2007 12:00:00 AM
2/2/2007 8:00:00 AM

...as it should be.
Ach. Too many replies to myself.

As I try it again, I see that a simple parse works fine:

string s = "2007-02-02 00:00 -8";
DateTime d = DateTime.Parse(s);
Console.WriteLine(d.Kind); // prints "Local"
Console.WriteLine(d); // prints "2/2/2007 12:00:00 AM"
Console.WriteLine(d.ToUniversalTime()); // prints "2/2/2007 8:00:00
AM"

Michael

Aug 1 '07 #4
ch****@totalise.co.uk wrote:
Hi,

I have a datetime+offset as a string and want to get two datetimes
from it; the local and UTC. So far I can only do it by splitting on
the + or - of the offset and creating a new date with and without the
offset. Is there a cleaner way, i.e., using System.DateTime etc. and
not resorting to string splitting? I had read about DateTimeOffset
which seems ideal but this is an ASP.Net 2.0 app and that seems to be
only in Framework 3.5 :(
How are you converting the strings now?

The Parse() and ParseExact() methods for DateTime include an overload
that takes a DateTimeStyles parameter. Using that parameter you can
specify the AdjustToUniversal style (flag), which will cause the
DateTime value formatted as a local time with offset to stored as a
universal time.

So, you can parse the string twice, once with the flag and once without
to get two different versions of the DateTime.

Pete
Aug 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christopher Boomer | last post by:
I am relatively new to the world of XML and python, and I think I am still missing some of the basic concepts. It cannot be as hard as I think, or programmers would be flocking to it the way they...
1
by: mbailey | last post by:
Hello- I have a Sql Server 2000 database offsite that I would like to back up to a local machine. I am using Enterprise Manager on a local machine to administer the remote db. Whats the best...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
2
by: JC Voon | last post by:
hi: Is there a function to format the datetime value to the following format 2005-09-29T17:57:03.0000000+08:00 ? currently i'm using a hard way Dim result As String
2
by: clintonG | last post by:
I haven't done this since ASP and though we could use some sort of "expression" to process code inline. I did some searching and reading but haven't found anything specific. What's up? <%=...
1
by: Rich Raffenetti | last post by:
I have an asp.net application that presents some user account information from Active Directory. I want to present current dates and times to the users in cases where the raw date/time is in GMT. ...
0
by: mavis | last post by:
I am using eclipse xsd packages to access xsd. What I am trying to do is: I have an element e1 (within a particle "p1") that refers to a global element e2, and then I want to make a copy of the...
2
by: ZR | last post by:
Hello, I need to convert a GMT time string to local time. I can fill out a "tm" structure with the GMT time string. Are there any standard C (or OS) time functions that will allow me to do this? ...
8
by: Hoi Wong | last post by:
With the XSLT 1.0 engine that I was forced to use, I have to parse old XML scripts where the number (to be parsed and saved into $EPISODE_NUMBER_RAW) that I want to parse is written with a comma...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.