Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.NET UInt32 to DateTime

Posted on 2012-03-22
8
Medium Priority
?
713 Views
Last Modified: 2012-06-21
Hello experts,

I have a UInt32 value that I need to convert to a DateTime

I have tried several methods and can't seem to get it to come out right.

my UInt32 value is 1327672399
and the decoded value should be
Friday, January 27 2012 08:53:19

I've been playing with the following to see if i could glean some useful insight into the conversion

Private Sub temporary(_uint As UInt32)
        '_uint = 1
        Dim _datetime As DateTime
        _datetime = DateTime.Now

        Debug.WriteLine("      TYPE            | SUCCESS | RESULT")
        Debug.WriteLine("Uint32 from DateTime = (" & UInt32.TryParse(Convert.ToString(_datetime), _uint) & ") " & Convert.ToString(_uint))
        Debug.WriteLine("DateTime from Uint32 = (" & DateTime.TryParse(Convert.ToString(_uint), _datetime) & ") " & _datetime)
    End Sub

Open in new window

0
Comment
Question by:sgaggerj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 37754005
try
Date.FromOADate("youstring")

Open in new window

0
 
LVL 8

Assisted Solution

by:davesgonebananas
davesgonebananas earned 400 total points
ID: 37754016
UInt32 unixTime = 1327672399;
System.DateTime date = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
date = dateTime.AddSeconds(unixTime);

Open in new window

0
 
LVL 38

Accepted Solution

by:
PaulHews earned 1520 total points
ID: 37754023
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        MsgBox(UnixToLocal(1327672399))
    End Sub

    Public Function UnixToLocal(ByVal UnixDateStamp As Long) As Date
        Dim dt As Date

        dt = #1/1/1970#.AddSeconds(UnixDateStamp)
        Return dt.ToLocalTime
    End Function
End Class

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sgaggerj
ID: 37754106
@nepaluz,

sorry but that doesn't help me.

@ davesgonebananas,

did you mean
date = date.AddSeconds(unixTime);

?

@ davesgonebananas, @PaulHews

I tried your solutions, they're essentially identical.

Using them, i input 1327672399 as the UInt32 value and got out
#1/27/2012 1:53:19 PM#

Any idea on the 5 hour difference between that and the expected result of
#1/27/2012 08:53:19#

The two values I'm using as comparisons are generated by another app written in c++ using

typedef __time64_t time_t;

Open in new window


I didn't write the other app.
0
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 1520 total points
ID: 37754145
local timesgaggerj,

Please check again.

Mine produces a local time, not the 1:53 PM time.  The difference is that simply converting the Unix epoch time stamp returns UTC time, which must be converted to local time.  In .NET it's as simple as calling .ToLocalTime() on the UTC time value.
0
 
LVL 1

Author Comment

by:sgaggerj
ID: 37754340
it's always the little things =)

You're right, that did work!

Then to reverse the process I'd do

Dim ts As TimeSpan = value.ToUniversalTime - New DateTime(1970, 1, 1, 0, 0, 0)
Dim v As UInt32 = CUInt(ts.TotalSeconds)

Open in new window


correct?
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 80 total points
ID: 37754364
UInt32 unixDate = 1327672399;
            DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            DateTime date = start.AddSeconds(unixDate).ToLocalTime();

Open in new window

0
 
LVL 38

Expert Comment

by:PaulHews
ID: 37754386
>Then to reverse the process I'd do

Yes, that looks right.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question