473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Convert DateTime to Unixdatetime

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?

Nov 21 '05 #1
10 4439
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
Nov 21 '05 #2
"Philip Wagenaar" <ph*************@online.nospam> schrieb
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

Nov 21 '05 #3
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?

"Cor Ligthert [MVP]" wrote:
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

Nov 21 '05 #4
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
"Armin Zingler" wrote:
"Philip Wagenaar" <ph*************@online.nospam> schrieb
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

Nov 21 '05 #5
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.

Nov 21 '05 #6
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(dt0Ocloc k)),TimeSpan).TotalSeconds.ToString

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

I hope this helps,

Cor
Nov 21 '05 #7
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
"Philip Wagenaar" <ph*************@online.nospam> schreef in bericht
news:BF**********************************@microsof t.com...
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?

"Cor Ligthert [MVP]" wrote:
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

Nov 21 '05 #8
That did the trick!

thank you.

""Peter Huang" [MSFT]" wrote:
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.

Nov 21 '05 #9
"Philip Wagenaar" <ph*************@online.nospam> schrieb
Armin I run it like this

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

and it returns 0.

Arguments are separated by blanks, thus the 2nd arg in CmdArgs(1) is
"1/1/1970" only.

Armin

Nov 21 '05 #10
Hi

You are welcomed!

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.

Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
3
by: Vicki Carlsen | last post by:
Hi, What is the easiest way to convert a DateTime object to a long?? - And the other way back?? (For database use) Regards, Vicki
5
by: simon | last post by:
I have datetime variable: Datetime tsEndTime; Should I use (DateTime): tsEndTime=(DateTime)rdr.GetValue(15) or is better to use: tsEndTime=Convert.ToDateTime(rdr.GetValue(15))
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
1
by: Ryan Ramsey | last post by:
I am trying to convert a value returned from the date() function in php 5.0 to a format .NET can use. DateTime dt_now = DateTime.Now; DateTime dt_last = new DateTime(Convert.ToInt32(dkpLast));...
2
by: kirke | last post by:
Hi, I have a datetime column named dtDateTime. its format is "Oct 27 2006 12:00:00 " I want to group by only date part of it and count my code is $sql1="SELECT ...
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.