Write a C# Sharp program to convert the value of the current DateTime object to Coordinated Universal Time (UTC)

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to convert the value of 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.

الأجوبة

using System;

class Example40
{
	static void Main()
	{
		DateTime localDateTime, univDateTime;

		Console.WriteLine("Enter a date and time.");
		string strDateTime = Console.ReadLine();

		try {
			localDateTime = DateTime.Parse(strDateTime);
    		univDateTime = localDateTime.ToUniversalTime();

    		Console.WriteLine("{0} local time is {1} universal time.",
   								localDateTime,
    								univDateTime); 
		}
		catch (FormatException) {
			Console.WriteLine("Invalid format.");
			return;
		}

		Console.WriteLine("Enter a date and time in universal time.");
		strDateTime = Console.ReadLine();

		try {
			univDateTime = DateTime.Parse(strDateTime);
    		localDateTime = univDateTime.ToLocalTime();

    		Console.WriteLine("{0} universal time is {1} local time.",
    								 univDateTime,
    								 localDateTime); 
		}
		catch (FormatException) {
			Console.WriteLine("Invalid format.");
			return;
		}

	}
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...