473,831 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(DateDiff(Dat eInterval.Secon d,
#1/1/1970#, dte))
Catch ex As Exception
Console.WriteLi ne(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 4482
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.WriteLi ne(DateDiff(Dat eInterval.Secon d, #1/1/1970#, dte))
Catch ex As Exception
Console.WriteLi ne(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.subtra ct(#1/1/1970#).totalsec onds
Armin

Nov 21 '05 #3
Cor try this with command line app:
Try
Dim dte As DateTime
dte = DateTime.Parse( CmdArgs(1))
Console.WriteLi ne(CType(dte.Su btract(New
System.DateTime (1970, 1, 1)), TimeSpan).Total Seconds.ToStrin g())
Catch ex As Exception
Console.WriteLi ne(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" /converttounixda te 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.WriteLi ne(CType(dte.Su btract(New
System.DateTime (1970, 1, 1)), TimeSpan).Total Seconds.ToStrin g())
Catch ex As Exception
Console.WriteLi ne(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.WriteLi ne(DateDiff(Dat eInterval.Secon d, #1/1/1970#, dte))
Catch ex As Exception
Console.WriteLi ne(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.subtra ct(#1/1/1970#).totalsec onds
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.Get CommandLineArgs ().

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).Total Seconds.ToStrin g)

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(dt8Ocloc k).Subtract(dt0 Oclock)),TimeSp an).TotalSecond s.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).Total Seconds.ToStrin g)

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******** *************** ***********@mic rosoft.com...
Cor try this with command line app:
Try
Dim dte As DateTime
dte = DateTime.Parse( CmdArgs(1))
Console.WriteLi ne(CType(dte.Su btract(New
System.DateTime (1970, 1, 1)), TimeSpan).Total Seconds.ToStrin g())
Catch ex As Exception
Console.WriteLi ne(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.Get CommandLineArgs ().

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" /converttounixda te 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

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

Similar topics

1
3252
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 value to a time value' error. It's related to glibc returning an error to a pre-1970 date, I think. My question: /how/ do I go through the Python direction I've created to remove the pre-1970 date objects? Ideally, I would be able to iterate...
1
1787
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 would be identical. I was 100% incorrect. The code below produces the results: CType Took: 0.2187528 seconds. Convert Took: 12.187656 seconds.
3
51731
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
18728
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
3796
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 List<RoleData> GetRoles() { return GetRoles(null, false); }
17
71486
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
2956
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)); TimeSpan dt_diff = dt_now.Subtract(dt_last); the value returned to dt_last is 1153455444 which is rougly July 20th ~ 10pm When I compare this to dt_diff I get a delta value (via dt_diff.Days) of
2
12011
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 convert(varchar,J1708Data.dtDateTime,120), count(convert(varchar,J1708Data.dtDateTime,120))
7
16733
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 returned from the source. My first attempt was as follows; (please ignore that error handling is not present in this example) public T GetValue<T(string objName) { T results;
0
9793
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10777
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10534
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10207
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9317
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.