How to convert Date to int

  • Thread starter Thread starter Terry Jolly
  • Start date Start date
T

Terry Jolly

New to C# ---- How do I convert a Date to int?

In VB6:

Dim lDate as long

lDate = CLng(Date)


In C#

int lDate;

Then what?


Thanks In Advance.
 
It should be noted that you are going to lose the time information when
you do this (and there is no way to prevent that, either).
 
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?
 
...
New to C# ---- How do I convert a Date to int?

In VB6:

Dim lDate as long

lDate = CLng(Date)


In C#

int lDate;

Then what?

Well, AFAIR, a Date in VB6 was internally represented by a double, where the
fractional part represented the time of day.

In .NET a DateTime isn't the same thing, and you won't get the same integer
value.

Here a DateTime has "ticks" which is a long, representing the number of
100-nanoseconds since the epoch (which date now that was).

So, the question is rather what you want it for, and how you're going to use
it?

If it simply is to store away it as an integer value, or vice versa, you can
use the ticks, e.g.:

DateTime d = new DateTime(632820056780400000);

long ticks = d.Ticks;

Note that it's not an int, it's a long.

// Bjorn A
 
private void button1_Click(object sender, EventArgs e)

{

int lDate;

lDate = Convert.ToInt32(System.DateTime.Now);

textBox1.Text = lDate.ToString();

}
 
The database i'm writing too stores dates as int32 --05/04/2006 would equal
38841. Just like MS Excel would display the a date if formated as a number
with zero decimal places.
 
Terry,

Try:


long lDate;

lDate = (long)System.DateTime.Now;

(OR lDate = Convert.ToInt64(System.DateTime.Now) )


You need a 64-bit variable in order to fit the DateTime into it.


Paul Cheetham
 
...
private void button1_Click(object sender, EventArgs e)
{

int lDate;

lDate = Convert.ToInt32(System.DateTime.Now);

textBox1.Text = lDate.ToString();

}


That won't work. This will:

private void button1_Click(object sender, EventArgs e)
{
long lDate = System.DateTime.Now.Ticks;
textBox1.Text = lDate.ToString();
}


But you still haven't explained what you need an integral value of the Date
for. That snippet doesn't make any sense...

// Bjorn A
 
The database i'm writing too stores dates as int32 --05/04/2006 would equal
38841. Just like MS Excel would display the a date if formated as a number
with zero decimal places.
 
Then it's the number of days since 1899-12-31? Use the Subtract method
to get a TimeSpan, and get the number of days:

DateTime date = DateTime.Today;
int days = date.Subtract(new DateTime(1899,12,31)).Days;
 
The database i'm writing too stores dates as int32 --05/04/2006 would equal
38841. Just like MS Excel would display the a date if formated as a number
with zero decimal places.
 
The database i'm writing too stores dates as int32 --05/04/2006 would equal
38841. Just like MS Excel would display the a date if formated as a number
with zero decimal places.
 
That's exactly what I'm trying to do. Thanks.

However, it will not compile even thought I have these imports:

import java.text.*;
import java.util.Date;
import java.util.Calendar;
import java.lang.Math;

Compile errors:

--------------------Configuration: Test2 - JDK version <Default> -
<Default>--------------------
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.java:10: cannot find symbol
symbol : class DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.java:10: cannot find symbol
symbol : variable DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.java:11: cannot find symbol
symbol : class DateTime
location: class Hello
int days = date.Subtract(new DateTime(1899,12,31)).Days;
^
3 errors

Process completed.
 
That is because it's C# code and you try to compile it as Java...
 

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

Back
Top