w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Long integer to valid time

Secondary Nav

C# Sharp DateTime : Exercise-22 with Solution

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.

Expected Output :

Invalid Time: False                                                              
3/14/2016 2:30:00 AM -> 3/14/2016 2:30:00 AM 

C# Sharp Code:

using System;
public class Example22
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2016, 3, 14, 2, 30, 00);
      Console.WriteLine("Invalid Time: {0}", 
                        TimeZoneInfo.Local.IsInvalidTime(date1));
      long ft = date1.ToFileTime();
      DateTime date2 = DateTime.FromFileTime(ft);
      Console.WriteLine("{0} -> {1}", date1, date2); 
   }
}

C# Sharp Practice online:

using System;
public class Example22
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2016, 3, 14, 2, 30, 00);
      Console.WriteLine("Invalid Time: {0}", 
                        TimeZoneInfo.Local.IsInvalidTime(date1));
      long ft = date1.ToFileTime();
      DateTime date2 = DateTime.FromFileTime(ft);
      Console.WriteLine("{0} -> {1}", date1, date2); 
   }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program