Convert DateTime to Unixdatetime

G

Guest

I have some code to convert a date to the unixdatetime representation (number
of seconds since 1970). Only problem is that it only counts from the whole
day, it ignores the times part. So convert 1/1/1970 and 1/1/1970 8:00:00 AM
both return 0 but the last should return 28800.

My code:
Try
Dim dte As DateTime
dte = DateTime.Parse(CmdArgs(1))
Console.WriteLine(DateDiff(DateInterval.Second,
#1/1/1970#, dte))
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

When I try to add 12:00:00 AM to the date in the DateDiff funcion vs.net
removes that.

Any ideas?
 
C

Cor Ligthert [MVP]

Phil,

I tested again the method I showed you in your previous question about that.

It shows 28800 with the values you show now.

I hope this helps,

Cor
 
A

Armin Zingler

Philip Wagenaar said:
I have some code to convert a date to the unixdatetime
representation (number of seconds since 1970). Only problem is that
it only counts from the whole day, it ignores the times part. So
convert 1/1/1970 and 1/1/1970 8:00:00 AM both return 0 but the last
should return 28800.

My code:
Try
Dim dte As DateTime
dte = DateTime.Parse(CmdArgs(1))

What *exactly* does cmdargs(1) contain?
Console.WriteLine(DateDiff(DateInterval.Second, #1/1/1970#, dte))
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

When I try to add 12:00:00 AM to the date in the DateDiff funcion
vs.net removes that.

12:00 AM is midnight. It's *display* is optional. #1/1/1970 12:00 AM# and
#1/1/1970# are identical values.
Any ideas?


Use the solution I already gave you some days ago:

dim diff as double

diff = yourdate.subtract(#1/1/1970#).totalseconds


Armin
 
G

Guest

Cor try this with command line app:
Try
Dim dte As DateTime
dte = DateTime.Parse(CmdArgs(1))
Console.WriteLine(CType(dte.Subtract(New
System.DateTime(1970, 1, 1)), TimeSpan).TotalSeconds.ToString())
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

Seems that conversion of commandline argument to datetime is going wrong.
Any thoughts on that?
 
G

Guest

Armin I run it like this

..exe" /converttounixdate 1/1/1970 4:00:00 am

and it returns 0.

The code now is:

Try
Dim dte As DateTime
dte = DateTime.Parse(CmdArgs(1))
Console.WriteLine(CType(dte.Subtract(New
System.DateTime(1970, 1, 1)), TimeSpan).TotalSeconds.ToString())
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
 
P

Peter Huang [MSFT]

Hi

I think you may try to enbrace the 1/1/1970 4:00:00 am into a "" , as
"1/1/1970 4:00:00 am".

Or it will be broken into three strings 1/1/1970, 4:00:00 ,am in the
Environment.GetCommandLineArgs().

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cor Ligthert [MVP]

Philip,

That is not the code I gave you.

Translated to this problem it is this

MessageBox.Show(CType(New Date(1970, 1, 1, 8, 0, 0).Subtract(New
System.DateTime(1970, 1, 1)), TimeSpan).TotalSeconds.ToString)

Or broken up
dim dt8Oclock as datetime = New Date(1970, 1, 1, 8, 0, 0)
dim dt0Oclock as datetiem = New Date(1970, 1, 1)

Messagebox.Show(CType(dt8Oclock).Subtract(dt0Oclock)),TimeSpan).TotalSeconds.ToString

I use the ISO times because that is in every culture the same.

I hope this helps,

Cor
 
C

Cor Ligthert [MVP]

Philip,

I was looking at the wrong code, so it is the code I gave you, however try
my sample that I added at the wrong place.

Translated to this problem it is this

MessageBox.Show(CType(New Date(1970, 1, 1, 8, 0, 0).Subtract(New
System.DateTime(1970, 1, 1)), TimeSpan).TotalSeconds.ToString)

I use the ISO times because that is in every culture the same.

And than to set your date to a string try for that
dte = CDate(CmdArgs(1))

My expirience is that using CDate helps to overcome a lot of problems.

I hope this helps,

If not reply than I try a test with setting the culture first (tell than
what that is).

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top