
1. Write a C# Sharp program to extract the Date property and display the DateTime value in formatted output. Go to the editor
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 demonstrate the Day property. Go to the editor
Expected Output :
year = 2016 month = 8 day = 16 hour = 3 minute = 57 second = 32 millisecond = 11
3. Write a C# Sharp program to demonstrate the DayOfWeek property and the System.DayOfWeek enumeration. Go to the editor
Expected Output :
The day of the week for 7/11/2016 is Monday.
4. Write a C# Sharp program to display the day of the year between two specified years. Go to the editor
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. Go to the editor
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 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. Go to the editor
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 17:02:10, Local UTC date and time: 20/08/2016 11:32:10, Utc English (New Zealand): Local date and time: 20/08/2016 5:02:10 PM, Local UTC date and time: 20/08/2016 11:32:10 AM, Utc français (Belgique): Local date and time: 20-08-16 17:02:10, Local UTC date and time: 20-08-16 11:32:10, Utc Deutsch (Schweiz): Local date and time: 20.08.2016 17:02:10, Local UTC date and time: 20.08.2016 11:32:10, Utc Nederlands (Nederland): Local date and time: 20-8-2016 17:02:10, Local UTC date and time: 20-8-2016 11:32:10, Utc
7. Write a C# Sharp program to display the value of the TimeOfDay property for an given array of DateTime values. Go to the editor
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 using the Date property. Go to the editor
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 the day of the week that is 40 days (960 hours) from this moment. Go to the editor
Expected Output :
Today = 8/20/2016 4:18:17 PM Thursday
10. Write a C# Sharp program to determine the day of the week 36 days after the current date. Go to the editor
Expected Output :
Today: Saturday 40 days from today: Thursday
11. Write a C# Sharp program to add a number of whole and fractional values to a date and time. Go to the editor
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 DateTime value and display each new value, the difference between it and the original value. Go to the editor
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. Go to the editor
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 between zero and fifteen months to the last day of August, 2016. Go to the editor
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 with a DateTime value that represents a leap year day. It displays the date for the fifteen years prior to and the fifteen years that follow February 29, 2016. Go to the editor
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. Go to the editor
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 the date one year in the future compare to current date. Go to the editor
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. Go to the editor
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. Go to the editor
Expected Output :
31 28 29
20. Write a C# Sharp Program to compare DateTime object. Go to the editor
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 using the specified array of formats, culture-specific format information, and style. Go to the editor
Expected Output :
0Unable 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 is then restored and the original value is adjusted to become a valid time. Go to the editor
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 Converts the specified Windows file time to an equivalent UTC time. Go to the editor
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.). Go to the editor
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. Go to the editor
Expected Output :
Friday, August 14, 2009
26. Write a C# Sharp Program to display the string representations of a date using the short date format specifier for the ja-JP culture. Go to the editor
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. Go to the editor
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 uses to find the leap years between 1994 and 2014 using IsLeapYear method. Go to the editor
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 converts 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. Go to the editor
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. Go to the editor
Expected Output :
Difference in days: 2253
31. Write a C# Sharp program to converts the value of the current DateTime object to local time. Go to the editor
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 value of the current DateTime object to its equivalent long date string representation. Go to the editor
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 value of the current DateTime object to its equivalent long time string representation. Go to the editor
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 value of the current DateTime object to its equivalent short date string representation. Go to the editor
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 converts the value of the current DateTime object to its equivalent short time string representation. Go to the editor
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 value of the current DateTime object to its equivalent string representation using the formatting conventions of the current culture. Go to the editor
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 displays the string representation of a date and time using CultureInfo objects that represent five different cultures. Go to the editor
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 uses these three format strings to display a date and time value by using the conventions of the en-CA and sv-FI cultures. Go to the editor
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 uses each of the standard date time format strings to display the string representation of a date and time for four different cultures. Go to the editor
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 converts the value of the current DateTime object to Coordinated Universal Time (UTC). Go to the editor
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. Go to the editor
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 using the specified format, culture-specific format information, and style. Go to the editor
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.
List of Exercises with Solutions :