DateTime and TimeSpan Instances
using System;
class MainClass {
public static void DisplayDateTime(string name, DateTime myDateTime) {
Console.WriteLine(name + " = " + myDateTime);
Console.WriteLine(name + ".Year = " + myDateTime.Year);
Console.WriteLine(name + ".Month = " + myDateTime.Month);
Console.WriteLine(name + ".Day = " + myDateTime.Day);
Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
Console.WriteLine(name + ".Second = " + myDateTime.Second);
Console.WriteLine(name + ".Millisecond = " + myDateTime.Millisecond);
Console.WriteLine(name + ".Ticks = " + myDateTime.Ticks);
}
public static void Main() {
int year = 2008;
int month = 8;
int day = 25;
DateTime myDateTime = new DateTime(year, month, day);
int hour = 23;
int minute = 30;
int second = 12;
int millisecond = 5;
DateTime myDateTime2 = new DateTime(year, month, day, hour, minute, second, millisecond);
System.Globalization.JulianCalendar myCalendar = new System.Globalization.JulianCalendar();
DateTime myDateTime3 = new DateTime(year, month, day, myCalendar);
DateTime myDateTime4 = new DateTime(0);
DisplayDateTime("myDateTime", myDateTime);
DisplayDateTime("myDateTime2", myDateTime2);
DisplayDateTime("myDateTime3", myDateTime3);
DisplayDateTime("myDateTime4", myDateTime4);
TimeSpan myTimeSpan = new TimeSpan(4, 12, 10);
myDateTime4 += myTimeSpan;
DisplayDateTime("myDateTime4", myDateTime4);
}
}
Related examples in the same category
1. | Current date and time | | |
2. | What day of the month is this? | | |
3. | Do some leap year checks | | |
4. | Look at the min and max date/time values | | |
5. | Output DateTime object | | |
6. | Constructors of DateTime | | |
7. | comparisons between DateTime objects | | |
8. | Parse and ParseExact | | |
9. | DateTime Now and its calculation | | |
10. | new DateTime(1900, 2, 29) | | |
11. | new DateTime(1900, 2, 29, new JulianCalendar()) | | |
12. | Specify Kind DateTime | | |
13. | Offset of DateTime | | |
14. | use the Now and UtcNow properties to get the currrent date and time | | |
15. | display the Date, Day, DayOfWeek, DayOfYear,Ticks, and TimeOfDayProperties of myDateTime | | |
16. | use the Compare() method to compare DateTime instances | | |
17. | use the overloaded less than operator (<) to compare two DateTime instances | | |
18. | use the Equals() method to compare DateTime instances | | |
19. | use the DaysInMonth() method to retrieve the number of days in a particular month and year | | |
20. | use the IsLeapYear() method to determine if a particular year is a leap year | | |
21. | use the Parse() method to convert strings to DateTime instances | | |
22. | use the Subtract() method to subtract a TimeSpan from a DateTime | | |
23. | use the overloaded subtraction operator (-) to subtract a TimeSpan from a DateTime | | |
24. | Illustrates the use of DateTime and TimeSpan instances | | |
25. | Displays the words 'Hello World!' on the screen, along with the current date and time | | |
26. | illustrates the use of TimeSpan properties and methods | | |
27. | A simple clock | | |
28. | Estimates pi by throwing points into a square. Use to
compare execution times | | |
29. | Change current culture and back | | |
30. | How values you passed into contructor mapped to its Properties | | |
31. | Gets the day of the week | | |
32. | Returns the number of days in the specified month and year. | | |
33. | DateTime and == | | |
34. | Create a DateTime structure with local time | | |
35. | Compare DateTime instance with Equal methods | | |
36. | Format a DateTime with M/dd/yyyy h:mm:ss.fff tt | | |
37. | Check the Kind of DateTime | | |
38. | DateTimeFormatInfo.CurrentInfo.TimeSeparator | | |
39. | Initializes a DateTime structure to a specified number of ticks. | | |
40. | DateTime.Now, DateTime.UtcNow, DateTime.Today | | |
41. | Parse a date time value from a string with Invariant Culture | | |
42. | Default value of DateTime is DateTime.MinValue | | |
43. | Default ToString output | | |
44. | Convert DateTime to string with fr-FR culture | | |
45. | ToString single letter format: F | | |
46. | DateTime ToString single letter format with fr-FR culture | | |
47. | DateTime Compare | | |
48. | DateTime CompareTo | | |
49. | Gets the date component of this instance. | | |
50. | Time String | | |
51. | Create day and year for a certain month | | |
52. | Returns the number of seconds since January 1, 1970 | | |