Solved

VB.NET UInt32 to DateTime

Posted on 2012-03-22
8
676 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 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

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!

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…

734 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