By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

How to convert Date to int

P: n/a
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.
May 4 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Try Convert.ToInt32(DateTime)

http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx

Jeff

Terry Jolly wrote:
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.

May 4 '06 #2

P: n/a
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).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
Try Convert.ToInt32(DateTime)

http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx

Jeff

Terry Jolly wrote:
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.

May 4 '06 #3

P: n/a
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
Try Convert.ToInt32(DateTime)

http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx

Jeff

Terry Jolly wrote:
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.

May 4 '06 #4

P: n/a
It would help if you paste a snippet of your code

Terry Jolly wrote:
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
Try Convert.ToInt32(DateTime)

http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx

Jeff

Terry Jolly wrote:
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.


May 4 '06 #5

P: n/a

"Terry Jolly" wrote...
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

May 4 '06 #6

P: n/a
private void button1_Click(object sender, EventArgs e)

{

int lDate;

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

textBox1.Text = lDate.ToString();

}

"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
It would help if you paste a snippet of your code

Terry Jolly wrote:
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
Try Convert.ToInt32(DateTime)

http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx

Jeff

Terry Jolly wrote:
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.


May 4 '06 #7

P: n/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.
"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
It would help if you paste a snippet of your code

Terry Jolly wrote:
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
Try Convert.ToInt32(DateTime)

http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx

Jeff

Terry Jolly wrote:
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.


May 4 '06 #8

P: n/a

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

Terry Jolly wrote:
private void button1_Click(object sender, EventArgs e)

{

int lDate;

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

textBox1.Text = lDate.ToString();

}

"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
It would help if you paste a snippet of your code

Terry Jolly wrote:
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
Try Convert.ToInt32(DateTime)

http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx

Jeff

Terry Jolly wrote:
> 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.

May 4 '06 #9

P: n/a

"Terry Jolly" wrote...
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
May 4 '06 #10

P: n/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.
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

"Terry Jolly" wrote...
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

May 4 '06 #11

P: n/a
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;

Terry Jolly wrote:
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.
"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
It would help if you paste a snippet of your code

Terry Jolly wrote:
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
Try Convert.ToInt32(DateTime)

http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx

Jeff

Terry Jolly wrote:
> 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.

May 4 '06 #12

P: n/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.

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

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

Terry Jolly wrote:
private void button1_Click(object sender, EventArgs e)

{

int lDate;

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

textBox1.Text = lDate.ToString();

}

"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
It would help if you paste a snippet of your code

Terry Jolly wrote:
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
> Try Convert.ToInt32(DateTime)
>
> http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx
>
> Jeff
>
> Terry Jolly wrote:
>> 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.

May 4 '06 #13

P: n/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.

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

"Terry Jolly" wrote...
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

May 4 '06 #14

P: n/a
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.jav a:10: cannot find symbol
symbol : class DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.jav a:10: cannot find symbol
symbol : variable DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.jav a:11: cannot find symbol
symbol : class DateTime
location: class Hello
int days = date.Subtract(new DateTime(1899,12,31)).Days;
^
3 errors

Process completed.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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;

Terry Jolly wrote:
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.
"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
It would help if you paste a snippet of your code

Terry Jolly wrote:
I get an error on compling: "Cannot implicitly convert type
'System.DateTime' to 'int' " ----- Any other ideas?

"Jeff" <ju******@jeff.net> wrote in message
news:PK*****************@tornado.texas.rr.com...
> Try Convert.ToInt32(DateTime)
>
> http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx
>
> Jeff
>
> Terry Jolly wrote:
>> 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.

May 4 '06 #15

P: n/a
That is because it's C# code and you try to compile it as Java...

Terry Jolly wrote:
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.jav a:10: cannot find symbol
symbol : class DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.jav a:10: cannot find symbol
symbol : variable DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.jav a:11: cannot find symbol
symbol : class DateTime
location: class Hello
int days = date.Subtract(new DateTime(1899,12,31)).Days;
^
3 errors

Process completed.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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;

Terry Jolly wrote:
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.
"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
It would help if you paste a snippet of your code

Terry Jolly wrote:
> I get an error on compling: "Cannot implicitly convert type
> 'System.DateTime' to 'int' " ----- Any other ideas?
>
>
>
> "Jeff" <ju******@jeff.net> wrote in message
> news:PK*****************@tornado.texas.rr.com...
>> Try Convert.ToInt32(DateTime)
>>
>> http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx
>>
>> Jeff
>>
>> Terry Jolly wrote:
>>> 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.


May 4 '06 #16

P: n/a
good grief...

Göran Andersson wrote:
That is because it's C# code and you try to compile it as Java...

Terry Jolly wrote:
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.jav a:10: cannot find
symbol
symbol : class DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.jav a:10: cannot find
symbol
symbol : variable DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.jav a:11: cannot find
symbol
symbol : class DateTime
location: class Hello
int days = date.Subtract(new DateTime(1899,12,31)).Days;
^
3 errors

Process completed.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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;

Terry Jolly wrote:
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.
"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
> It would help if you paste a snippet of your code
>
> Terry Jolly wrote:
>> I get an error on compling: "Cannot implicitly convert type
>> 'System.DateTime' to 'int' " ----- Any other ideas?
>>
>>
>>
>> "Jeff" <ju******@jeff.net> wrote in message
>> news:PK*****************@tornado.texas.rr.com...
>>> Try Convert.ToInt32(DateTime)
>>>
>>> http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx
>>>
>>> Jeff
>>>
>>> Terry Jolly wrote:
>>>> 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.


May 4 '06 #17

P: n/a
Work's perfect --- many thanks!
"Göran Andersson" <gu***@guffa.com> wrote in message
news:eP**************@TK2MSFTNGP04.phx.gbl...
That is because it's C# code and you try to compile it as Java...

Terry Jolly wrote:
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.jav a:10: cannot find
symbol
symbol : class DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.jav a:10: cannot find
symbol
symbol : variable DateTime
location: class Hello
DateTime date = DateTime.Today;
^
C:\Program Files\Xinox
Software\JCreatorV3\MyProjects\Test2\src\Hello.jav a:11: cannot find
symbol
symbol : class DateTime
location: class Hello
int days = date.Subtract(new DateTime(1899,12,31)).Days;
^
3 errors

Process completed.

"Göran Andersson" <gu***@guffa.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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;

Terry Jolly wrote:
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.
"Jeff" <ju******@jeff.net> wrote in message
news:Ke*****************@tornado.texas.rr.com...
> It would help if you paste a snippet of your code
>
> Terry Jolly wrote:
>> I get an error on compling: "Cannot implicitly convert type
>> 'System.DateTime' to 'int' " ----- Any other ideas?
>>
>>
>>
>> "Jeff" <ju******@jeff.net> wrote in message
>> news:PK*****************@tornado.texas.rr.com...
>>> Try Convert.ToInt32(DateTime)
>>>
>>> http://msdn2.microsoft.com/en-us/lib...t.toint32.aspx
>>>
>>> Jeff
>>>
>>> Terry Jolly wrote:
>>>> 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.


May 4 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.