«

»

Dec 20

Converting Date to and from UTC in PowerBuilder.NET & .NET Framework

Rich thanks you for sharing...




Converting from UTC to Local & Local to UTC using .NET Framework

 

Converting dates from or to UTC in the .NET Framework is pretty simple as the DateTime class has many built in features including many conversion related functions.

 

Notice

When you parse a string into a date you will need to be careful in knowing what time zone the date and time string is from but assuming you know what date time you are working with you simply use the SpecifyKind method of the DateTime class.

 

Example .NET Framework Date Conversion Code ( UTC / Local )

The .NET code for performing the UTC to local and back conversion is listed below.

 

// Get date for current moment adjusted for local time zone.
DateTime utcNow = DateTime.UtcNow;
DateTime localNow = DateTime.Now;

// convert the UTC date to Local
newDate = DateTime.SpecifyKind( utcNow, DateTimeKind.Local );
// convert the Local time to UTC
newDate = DateTime.SpecifyKind( localNow, DateTimeKind.Utc );

 

  • http://dotnetforge.com .NET Framework

    I’m not seeing anything better than brute force – parse to the second
    space and Date(), parse to the next space and Time(), DateTime().
    There are APIs that you can use for the local time zone if you need to
    get to there given the UTC offset, and to retrieve the local offset. But
    I’m not seeing a Windows representation that matches your format, so I’d
    stay with the PB functions just for clarity.

    Report Bugs to Sybase: http://case-express.sybase.com/cx/welcome.do

    Product Enhancement Requests: http://my.isug.com/p/cm/ld?fid=187
    .NET Framework recently posted..Big Data Analytics Using SplunkMy Profile

  • http://www.ub.edu/edap/index.php?option=com_blog&view=comments&pid=86588&Itemid=0&lang=en Carolyn

    Your overall success when writing for people as a blogger
    will rely in part upon being able to take ‘old’ news and present it
    in a ‘new’ way. Even if you are only adding a few paragraphs, try to
    update it at least every couple of days.
    Carolyn recently posted..CarolynMy Profile

  • Mark

    Hi Rich,

    I had to do the same thing for a system. The datetime values are stored in the db as Utc. Several months ago I created a class to handle the conversions on the client. I created two methods called ToUtc and ToLocal. ToUtc checks the incoming datetime’s Kind property and if its not Utc calls ToUniversalTime. ToLocal checks the incoming datetime’s Kind property and if its not Local calls ToLocalTime. I probably could have saved myself some time if I’d noticed the SpecifyKind method for the Datetime class.

    Thanks for sharing,
    Mark