Solved

VB.NET UInt32 to DateTime

Posted on 2012-03-22
8
641 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
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 100 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 380 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 380 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 20 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now