Solved

VB.NET UInt32 to DateTime

Posted on 2012-03-22
8
682 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
Technology Partners: 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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