Groups | Blog | Home
all groups > c# > april 2004 >

c# : Urgent date question


Dinçer
4/11/2004 10:33:48 PM
I need to compare dates. But this way:

I get a date from database.
I need to understand if this date is newer then "6 months" or not.

What I need is get the current date,
calculate the date 6 months ago,
compare "the date from DB" & "the date 6 months ago from now"
show the result.

How will I do that using DateTime or TimeSpan classes?

Andreas Håkansson
4/11/2004 10:52:57 PM

The different Add-methods on the DateTime class accepts negative
values. To subtract 6 months of the current date, you would do like this

DateTime today = DateTime.Now;
DateTime then = today.AddMonths(-6);

The variable 'then' will now contain the date you are looking for.

HTH,

//Andreas

"Dinçer" <dincer80"at"yahoo.com> skrev i meddelandet
news:%23yd23u$HEHA.3032@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Andreas Håkansson
4/11/2004 11:11:43 PM
Dinçer,

The DateTime structure has two static members which should be of
intresst to you. They are DateTime.Parse() and DateTime.ParseExact(),
and I recommend you read the documentation for them.

HTH,

//Andreas

"Dinçer" <dincer80"at"yahoo.com> skrev i meddelandet
news:OfkaehAIEHA.716@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

Dinçer
4/12/2004 12:04:21 AM
Thank you.
One last question:

The type of the date I get from DB is String.
How can I convert it to DateTime. (So that I can make the comparison)

DateTime constructer has no String argument. How will I do this?




[quoted text, click to view]

Dinçer
4/12/2004 12:19:14 AM
OK.

Thank you..


[quoted text, click to view]

Jon Skeet [C# MVP]
4/12/2004 8:21:06 AM
[quoted text, click to view]

Along with the other answers you've received, I have to at least=20
suggest that if you have any control over the database whatsoever, then=20
you change the type of the column to be a date type. It will make your=20
life *far* easier. I realise you may not have control over the=20
database, but if you do, it's definitely something to think about=20
changing.

--=20
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button