C# Sharp DateTime : Exercises, Practice, Solution
C# Sharp DateTime [57 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a C# Sharp program to extract the Date property and display the DateTime value in the formatted output.
Expected Output:
Complete date: 6/8/2016 11:49:00 AM Short Date: 6/8/2016 Display date using 24-hour clock format: 6/8/2016 12:00 AM 06/08/2016 00:00
2. Write a C# Sharp program to display the Day properties (year, month, day, hour, minute, second, millisecond etc.).
Expected Output :
year = 2016 month = 8 day = 16 hour = 3 minute = 57 second = 32 millisecond = 11
3. Write a C# Sharp program to get the day of the week for a specified date.
Expected Output :
The day of the week for 7/11/2016 is Monday.
4. Write a C# Sharp program to display the number of days of the year between two specified years.
Expected Output :
12/31/2001: day 365 of 2001 12/31/2002: day 365 of 2002 12/31/2003: day 365 of 2003 12/31/2004: day 366 of 2004 (Leap Year) 12/31/2005: day 365 of 2005 12/31/2006: day 365 of 2006 12/31/2007: day 365 of 2007 12/31/2008: day 366 of 2008 (Leap Year) .......
5. Write a C# Sharp program to get a DateTime value that represents the current date and time on the local computer.
Expected Output :
English (Ireland): Local date and time: 20/08/2016 15:49:03, Local UTC date and time: 20/08/2016 10:19:03, Utc English (South Africa): Local date and time: 2016-08-20 03:49:03 PM, Local UTC date and time: 2016-08-20 10:19:03 AM, Utc ......
6. Write a C# Sharp program to display the number of ticks that have elapsed since the beginning of the twenty-first century and to instantiate a TimeSpan object using the Ticks property.
Note: The TimeSpan object is then used to display the elapsed time using several other time intervals.
Expected Output :
English (Jamaica): Local date and time: 20/08/2016 15:54:07, Local UTC date and time: 20/08/2016 10:24:07, Utc English (New Zealand): Local date and time: 20/08/2016 3:54:07 PM, Local UTC date and time: 20/08/2016 10:24:07 AM, Utc suomi (Suomi): Local date and time: 20.8.2016 15:54:07, Local UTC date and time: 20.8.2016 10:24:07, Utc Deutsch (Schweiz): Local date and time: 20.08.2016 15:54:07, Local UTC date and time: 20.08.2016 10:24:07, Utc Nederlands (Nederland): Local date and time: 20-8-2016 15:54:07, Local UTC date and time: 20-8-2016 10:24:07, Utc
7. Write a C# Sharp program to get the time of day from a given array of date time values.
Array of DateTime : DateTime[] dates = { DateTime.Now, new DateTime(2016, 8, 16, 9, 28, 0), new DateTime(2011, 5, 28, 10, 35, 0), new DateTime(1979, 12, 25, 14, 30, 0) };
Expected Output :
Day: 8/20/2016 Time: 15:58:26.3566320 Day: 8/20/2016 Time: 3:58 PM Day: 8/16/2016 Time: 09:28:00 Day: 8/16/2016 Time: 9:28 AM Day: 5/28/2011 Time: 10:35:00 Day: 5/28/2011 Time: 10:35 AM Day: 12/25/1979 Time: 14:30:00 Day: 12/25/1979 Time: 2:30 PM
8. Write a C# Sharp program to retrieve the current date.
Expected Output :
General format 8/20/2016 12:00:00 AM Display the date in a variety of formats: 8/20/2016 Saturday, August 20, 2016 8/20/2016 12:00 AM
9. Write a C# Sharp program to calculate what day of the week is 40 days from now.
Expected Output :
Today = 8/20/2016 4:18:17 PM Thursday
10. Write a C# Sharp program to determine the day of the week 40 days after the current date.
Expected Output :
Today: Saturday 40 days from today: Thursday
11. Write a C# Sharp program to add whole and fractional values to a date and time.
Expected Output :
8/16/2016 12:00:00 PM + 0.08333 hour(s) = 8/16/2016 12:04:59 PM 8/16/2016 12:00:00 PM + 0.16667 hour(s) = 8/16/2016 12:10:00 PM 8/16/2016 12:00:00 PM + 0.25 hour(s) = 8/16/2016 12:15:00 PM 8/16/2016 12:00:00 PM + 0.33333 hour(s) = 8/16/2016 12:19:59 PM 8/16/2016 12:00:00 PM + 0.5 hour(s) = 8/16/2016 12:30:00 PM 8/16/2016 12:00:00 PM + 0.66667 hour(s) = 8/16/2016 12:40:00 PM 8/16/2016 12:00:00 PM + 1 hour(s) = 8/16/2016 1:00:00 PM 8/16/2016 12:00:00 PM + 2 hour(s) = 8/16/2016 2:00:00 PM 8/16/2016 12:00:00 PM + 29 hour(s) = 8/17/2016 5:00:00 PM 8/16/2016 12:00:00 PM + 30 hour(s) = 8/17/2016 6:00:00 PM 8/16/2016 12:00:00 PM + 31 hour(s) = 8/17/2016 7:00:00 PM 8/16/2016 12:00:00 PM + 90 hour(s) = 8/20/2016 6:00:00 AM 8/16/2016 12:00:00 PM + 365 hour(s) = 8/31/2016 5:00:00 PM
12. Write a C# Sharp Program to add one millisecond and 2.5 milliseconds to a given date value. Display each value and the difference between it and the original value.
Note: The difference is displayed both as a time span and as a number of ticks and one millisecond equals 10,000 ticks.
Expected Output :
Original date: 08/16/2016 04:00:00.0000000 (636,069,600,000,000,000 ticks) Second date: 08/16/2016 04:00:00.0010000 (636,069,600,000,010,000 ticks) Difference between dates: 00:00:00.0010000 (10,000 ticks) Third date: 08/16/2016 04:00:00.0025000 (636,069,600,000,025,000 ticks) Difference between dates: 00:00:00.0025000 (25,000 ticks)
13. Write a C# Sharp Program to add 30 seconds and the number of seconds in one day to a DateTime value.
Note: It displays each new value and displays the difference between it and the original value. The difference is displayed both as a time span and as a number of ticks.
Expected Output :
Original date: 08/15/2016 04:00:00 (636,068,736,000,000,000 ticks) Second date: 08/15/2016 04:00:30 (636,068,736,300,000,000 ticks) Difference between dates: 00:00:30 (300,000,000 ticks) Third date: 08/16/2016 04:00:00 (636,069,600,000,000,000 ticks) Difference between dates: 1.00:00:00 (864,000,000,000 ticks)
14. Write a C# Sharp program to add a specified number of months (between zero and fifteen months) to the last day of August, 2016.
Expected Output :
8/31/2016 9/30/2016 10/31/2016 11/30/2016 12/31/2016 1/31/2017 2/28/2017 3/31/2017 4/30/2017 5/31/2017 6/30/2017 7/31/2017 8/31/2017 9/30/2017 10/31/2017 11/30/2017
15. Write a C# Sharp program to display the date of the past and future fifteen years from a specified date.
Expected Output :
Base Date: 2/29/2016 1 year(s) ago: 2/28/2015 2 year(s) ago: 2/28/2014 3 year(s) ago: 2/28/2013 4 year(s) ago: 2/29/2012 5 year(s) ago: 2/28/2011 .......
16. Write a C# Sharp program that compares two dates.
Expected Output :
8/1/2016 12:00:00 AM is earlier than 8/1/2016 12:00:00 PM
17. Write a C# Sharp Program to create a date one year previously and one year in the future compared to the current date.
Expected Output :
1: 8/20/2016 is later than 8/20/2015 -1: 8/20/2016 is earlier than 8/20/2017
18. Write a C# Sharp program to compare the current date with a given date.
Expected Output :
7/28/2016 is in the past.
19. Write a C# Sharp program to get the number of days in the specified month and year.
Expected Output:
31 28 29
20. Write a C# Sharp program to compare DateTime objects.
Expected Output:
The result of comparing DateTime object one and two is: False. The result of comparing DateTime object one and three is: True.
21. Write a C# Sharp program to convert the specified string representation of a date and time to its DateTime equivalent. This is done using the specified array of formats, culture-specific format information, and style.
Expected Output:
Unable to convert '8/1/2016 6:32 PM' to a date. Unable to convert '08/01/2016 6:32:05 PM' to a date. Converted '8/1/2016 6:32:00' to 8/1/2016 6:32:00 AM. Converted '08/01/2016 06:32' to 8/1/2016 6:32:00 AM. Unable to convert '08/01/2016 06:32:00 PM' to a date. Converted '08/01/2016 06:32:00' to 8/1/2016 6:32:00 AM.
22. Write a C# Sharp program which shows that when a time that falls within this range is converted to a long integer value and restored and the original value is adjusted to become a valid time.
Expected Output :
Invalid Time: False 3/14/2016 2:30:00 AM -> 3/14/2016 2:30:00 AM
23. Write a C# Sharp program to convert the specified Windows file time to an equivalent UTC time.
Expected Output :
6/2/1639 9:25:12 AM
24. Write a C# Sharp program to display the string representation of a date using all possible standard date and time formats in the computer's current culture (en-US).
Expected Output :
7/28/2009 7/28/09 07/28/09 07/28/2009 09/07/28 .......
25. Write a C# Sharp program to display the string representation of a date using the long date format.
Expected Output :
Friday, August 14, 2009
26. Write a C# Sharp program to display string representation of a date using the short date format specified for ja-JP culture.
Expected Output :
2016/05/12 16/05/12 16/5/12 2016/5/12 ......
27. Write a C# Sharp program to determine the type of a particular object.
Expected Output :
24 is a 32-bit integer. 10653 is a 32-bit integer. 24 is an unsigned byte. -5 is a signed byte. 26.3 is a double-precision floating point. 'string' is another data type.
28. Write a C# Sharp program to find the leap years between 1994 and 2014.
Note : Use IsLeapYear method.
Expected Output :
1996 is a leap year. One year from 2/29/1996 is 2/28/1997. 2000 is a leap year. One year from 2/29/2000 is 2/28/2001. 2004 is a leap year. One year from 2/29/2004 is 2/28/2005. ......
29. Write a C# Sharp program to convert the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style.
Expected Output :
Converted '5/2/2009 6:32 PM' to 5/2/2009 6:32:00 PM. Converted '05/02/2009 6:32:05 PM' to 5/2/2009 6:32:05 PM. Converted '5/2/2009 6:32:00' to 5/2/2009 6:32:00 AM. Converted '05/02/2009 06:32' to 5/2/2009 6:32:00 AM. ......
30. Write a C# Sharp program to get the difference between two dates in days.
Expected Output :
Difference in days: 2253
31. Write a C# Sharp program to convert the current DateTime object value to local time.
Expected Output :
Enter a date and time. 1:57 8/23/2016 1:57:00 AM local time is 8/22/2016 8:27:00 PM universal time. Enter a date and time in universal time. 8:27:00 8/23/2016 8:27:00 AM universal time is 8/23/2016 1:57:00 PM local time.
32. Write a C# Sharp program to convert the current DateTime object value to its equivalent long date string representation.
Expected Output :
Initialize the DateTime object to May 16, 2016 3:02:15 AM. The date and time patterns are defined in the DateTimeFormatInfo object associated with the current thread culture. Current culture: "en-US" Long date pattern: "dddd, MMMM d, yyyy" Long date string: "Monday, May 16, 2016" .........
33. Write a C# Sharp program to convert the current DateTime object value to its equivalent long time string representation.
Expected Output :
Current culture: en-CA Date: 2016-08-17 10:30:15 AM Long time pattern: 'h:mm:ss tt' Long time with format string: 10:30:15 AM Long time with ToLongTimeString: 10:30:15 AM ........
34. Write a C# Sharp program to convert the current DateTime object value to its equivalent short date string representation.
Expected Output :
Displaying short date for en-NZ culture: 17/08/2016 (Short Date String) 17/08/2016 ('d' standard format specifier) Displaying short date for fi-FI culture: 17.8.2016 .......
35. Write a C# Sharp program to convert the current DateTime object value to its equivalent short time string representation.
Expected Output :
Initialize the DateTime object to August 16, 2016 3:02:15 AM. The date and time patterns are defined in the DateTimeFormatInfo object associated with the current thread culture. Current culture: "en-US" Long date pattern: "dddd, MMMM d, yyyy" Long date string: "Tuesday, August 16, 2016" ......
36. Write a C# Sharp program to convert the current DateTime object value to its equivalent string representation. This is done using the current culture's formatting conventions.
Expected Output :
5/16/2016 6:32:06 PM 16/05/2016 18:32:06 2016/05/16 18:32:06
37. Write a C# Sharp program to display the string representation of a date and time using CultureInfo objects that represent five different cultures.
Expected Output :
In Invariant Language (Invariant Country), 05/17/2016 09:00:00 In en-ZA, 2016-05-17 09:00:00 AM In ko-KR, 2016-05-17 오전 9:00:00 In de-DE, 17.05.2016 09:00:00 In es-ES, 17/05/2016 9:00:00 In en-US, 5/17/2016 9:00:00 AM
38. Write a C# Sharp program to use these three format strings to display a date and time value. This is done by using the conventions of the en-CA and SV-FI cultures.
formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm", "yyyyMMdd" }
Expected Output :
English (Canada) G: 5/17/2016 1:31:17 PM MM/yyyy: 05/2016 MM\/dd\/yyyy HH:mm: 05/17/2016 13:31 yyyyMMdd: 20160517 ......
39. Write a C# Sharp program to use each of the standard date and time format strings to display the string representation of a date and time. This is for four different cultures.
Expected Output :
d Format Specifier zh-HK Culture 16/10/2015 d Format Specifier en-US Culture 10/16/2015 d Format Specifier es-ES Culture 16/10/2015 d Format Specifier fr-CA Culture 2015-10-16 D Format Specifier zh-HK Culture 2015年10月16日 D Format Specifier en-US Culture Friday, October 16, 2015 D Format Specifier es-ES Culture viernes, 16 de octubre de 2015 D Format Specifier fr-CA Culture 16 octobre 2015 ........
40. Write a C# Sharp program to convert the current DateTime object to Coordinated Universal Time (UTC).
Expected Output :
Enter a date and time. 08/22/2016 02:30 8/22/2016 2:30:00 AM local time is 8/21/2016 9:00:00 PM universal time. Enter a date and time in universal time. 08/22/2016 02:50 8/22/2016 2:50:00 AM universal time is 8/22/2016 8:20:00 AM local time.
41. Write a C# Sharp program to convert the specified string representation of a date and time to its DateTime equivalent.
Expected Output :
Attempting to parse strings using en-US culture. Converted '05/01/2016 14:57:32.8' to 5/1/2016 2:57:32 PM (Unspecified). Converted '2016-05-01 14:57:32.8' to 5/1/2016 2:57:32 PM (Unspecified). Converted '2016-05-01T14:57:32.8375298-04:00' to 5/2/2016 12:27:32 AM (Local). Converted '5/01/2015' to 5/1/2015 12:00:00 AM (Unspecified). Converted '5/01/2015 14:57:32.80 -07:00' to 5/2/2015 3:27:32 AM (Local). Converted '1 May 2015 2:57:32.8 PM' to 5/1/2015 2:57:32 PM (Unspecified). Unable to parse '16-05-2016 1:00:32 PM'. Unable to parse 'Fri, 15 May 2016 20:10:57 GMT'.
42. Write a C# Sharp program to convert the specified string representation of a date and time to its DateTime equivalent. This is done using the specified format, culture-specific format information, and style.
Expected Output :
05/06/2016 10:00 AM converted to 6/5/2016 10:00:00 AM Unspecified. 05/06/2016 10:00 AM converted to 6/5/2016 10:00:00 AM Local. 2016/05/06T10:00:00-5:00 converted to 5/6/2016 8:30:00 PM Local. Unable to convert 05/06/2016T10:00:00-5:00 to a date and time. 2015-05-06 10:00 converted to 5/6/2015 4:30:00 AM Utc.
43. Write a program in C# Sharp to check whether the given year, month and day are current or not.
Test Data :
Input the Day : 17
Input the Month : 09
Input the Year : 2016
Expected Output :
The formatted Date is : 17/09/2016 The current date status : True
44. Write a program in C# Sharp to compute yesterday's date.
Expected Output :
Today is : 17/09/2016 The Yesterday was : 16/09/2016
45. Write a program in C# Sharp to compute tomorrow's day.
Expected Output :
Today is : 17/09/2016 The Tomorrow will be : 18/09/2016
46. Write a program in C# Sharp to get the first day of a year against a date.
Test Data:
Input the Day : 17
Input the Month : 09
Input the Year : 2016
Expected Output :
The formatted Date is : 12/12/2012 The First day of the year 2012 is : 01/01/2012
47. Write a program in C# Sharp to get the last day of the current year against a given date.
Test Data:
Input the Day : 12
Input the Month : 12
Input the Year : 2012
Expected Output :
The formatted Date is : 12/12/2012 The Last day of the year 2012 is : 31/12/2012
48. Write a program in C# Sharp to get the number of days in a given month for a year.
Test Data:
Input the Month No. : 2
Input the Year : 2017
Expected Output :
The Number of days in the month February is : 28
49. Write a program in C# Sharp to get the day and month of the current date.
Expected Output :
The current Month No. is : 9 The current Month is : September
50. Write a C# Sharp program to print the name of the first three letters of each month of a year starting from current date.
Expected Output :
The twelve months are : Sep Oct Nov Dec Jan Feb Mar Apr May Jun Jul Aug
51. Write a C# Sharp program to print the month name in full starting from the current date.
Expected Output :
Display the name of the months of a year : ----------------------------------------------- The date of Today : 13/06/2017 The twelve months are : June July August September October November December January February March April May
52. Write a program in C# Sharp to find the first day of a week against a given date.
Test Data:
Input the Day : 16
Input the Month : 06
Input the Year : 2016
Expected Output :
The formatted Date is : 16/06/2016 The first day of the week for the above date is: 12/06/2016
53. Write a program in C# Sharp to find the last day of a week against a given date.
Test Data:
Input the Day : 16
Input the Month : 06
Input the Year : 2016
Expected Output :
The formatted Date is : 16/06/2016 The last day of the week for the above date is: 18/06/2016
54. Write a program in C# Sharp to find the first day of the month against a given date.
Test Data:
Input the Day : 16
Input the Month : 06
Input the Year : 2016
Expected Output :
The formatted Date is : 16/06/2016 The first day of the month for the above date is: 01/06/2016
55. Write a program in C# Sharp to find the last day of a month against a given date.
Test Data:
Input the Day : 16
Input the Month : 06
Input the Year : 2016
Expected Output :
The formatted Date is : 16/06/2016 The last day of the month for the above date is: 30/06/2016
56. Write a C# Sharp program to find the first day of the next month against a given date.
Test Data:
Input the Day : 16
Input the Month : 06
Input the Year : 2016
Expected Output :
The formatted Date is : 16/06/2016 The first day of the next month for the above date is: 01/07/2016
57. Write a program in C# Sharp to find the day on a particular date.
Test Data:
Input the Day : 16
Input the Month : 06
Input the Year : 2016
Expected Output :
The formatted Date is : 16/06/2016 The day for the date is : Thursday
C# Sharp Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics