- I need format type DateTime into: "dd/mm/yyyy".
- I want to add, sub, compare between 2 DateTime. ex: 23/12/1991 > 2/1/1990. 23/12/1991 - 20(days) = 3/12/1991 Could you help me, please.! Thank very much.! ^^
Asked
Active
Viewed 537 times
0

Võ Hoài Lên
- 69
- 1
- 8
3 Answers
2
To get that format use:
yourDate.ToString("dd/MM/yyyy);
To add to a date:
yourDate.AddDays(15);
yourDate.AddMonths(3);
and so on
To subtract from a date
yourDate.AddDays(-12);
yourDate.AddMonths(-3);
and so on
And any date objects can be compared with the normal > < <= >=
operators.

Adam Rackis
- 82,527
- 56
- 270
- 393
-
I need get random DateTime between 2 Days. ex: between 1/2/2003 and 2/3/2003??? Thank for help me.! – Võ Hoài Lên Dec 15 '11 at 03:35
-
@VõHoàiLên - to get a random date, have a look at this http://stackoverflow.com/questions/194863/random-date-in-c-sharp – Adam Rackis Dec 15 '11 at 03:40
0
Format:
dateTime.ToString("dd/MM/yyyy");
Add and Sub: Look at the different overload of DateTime.Add and the various others (AddDays, AddHours, etc).
Compare:
dateTime1 - dateTime2
That will return a Timespan. You can thus do:
(dateTime1-dateTime2).Days >= 20

MPelletier
- 16,256
- 15
- 86
- 137
0
To format the string representation if you can do the following:
var date = DateTime.Now;
var dateString = date.ToString("dd/MM/yyyy");
In order the add/sub days to a DateTime object, use the AddDays()
method:
// Subtract 20 days
var date = DateTime.Now;
var twentyDaysAgo = date.AddDays(-20);
There is also an AddMonths()
method that works in the same way.

Phil Klein
- 7,344
- 3
- 30
- 33