w3resource

C#: Add a number of whole and fractional values to a date and time

C# Sharp DateTime : Exercise-11 with Solution

Write a C# Sharp program to add whole and fractional values to a date and time.

Sample Solution:-

C# Sharp Code:

using System;

public class Example11
{
    // Main method, entry point of the program
    public static void Main()
    {
        // An array of double values representing different hours
        double[] hours = {.08333, .16667, .25, .33333, .5, .66667, 1, 2, 
                          29, 30, 31, 90, 365};

        // Create a DateTime object with specific date and time (August 16, 2016, 12:00:00 PM)
        DateTime dateValue = new DateTime(2016, 8, 16, 12, 0, 0);

        // Iterate through each hour in the array
        foreach (double hour in hours)
        {
            // Display the original date and time along with the addition of 'hour' hours
            Console.WriteLine("{0} + {1} hour(s) = {2}", dateValue, hour, dateValue.AddHours(hour));
        }
    }
}

Sample 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 

Flowchart :

Flowchart: C# Sharp Exercises - Add a number of whole and fractional values to a date and time.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write C# Sharp program to determine the day of the week 40 days after the current date.
Next: Write C# Sharp Program to add one millisecond and 2.5 milliseconds to a given date value and display each new value and the difference between it and the original value.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.