[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.NET UInt32 to DateTime

Posted on 2012-03-22
8
Medium Priority
?
726 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

873 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