Urgent date question

  • Thread starter Thread starter Dinçer
  • Start date Start date
D

Dinçer

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?
 
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
 
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?
 
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
 
OK.

Thank you..


Andreas Håkansson said:
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
 
The type of the date I get from DB is String.

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