PC Review


Reply
Thread Tools Rate Thread

Convert DateTime to Unixdatetime

 
 
=?Utf-8?B?UGhpbGlwIFdhZ2VuYWFy?=
Guest
Posts: n/a
 
      23rd Aug 2005
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?

 
Reply With Quote
 
 
 
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      23rd Aug 2005
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


 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      23rd Aug 2005
"Philip Wagenaar" <(E-Mail Removed)> 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

 
Reply With Quote
 
=?Utf-8?B?UGhpbGlwIFdhZ2VuYWFy?=
Guest
Posts: n/a
 
      23rd Aug 2005
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
>
>
>

 
Reply With Quote
 
=?Utf-8?B?UGhpbGlwIFdhZ2VuYWFy?=
Guest
Posts: n/a
 
      23rd Aug 2005
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" <(E-Mail Removed)> 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
>
>

 
Reply With Quote
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      23rd Aug 2005
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.

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      23rd Aug 2005
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


 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      23rd Aug 2005
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" <(E-Mail Removed)> schreef in bericht
news:BFEDDC65-691F-46F9-B37C-(E-Mail Removed)...
> 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
>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?UGhpbGlwIFdhZ2VuYWFy?=
Guest
Posts: n/a
 
      23rd Aug 2005
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.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DateTime shapper Microsoft ASP .NET 0 30th Sep 2006 01:48 AM
Datetime convert SimonZ Microsoft C# .NET 2 22nd Dec 2005 01:59 PM
System.DateTime doesn't convert to SqlDbType.DateTime Narshe Microsoft C# .NET 3 2nd Nov 2005 05:42 PM
DateTime convert Ha ha Microsoft C# .NET 7 23rd Dec 2004 03:43 PM
Convert datetime to System.DateTime Martin Schmid Microsoft ADO .NET 1 24th Feb 2004 08:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:34 PM.