- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
DateTime.ToUniversalTime() Method in C#
The DateTime.ToUniversalTime() method in C# is used to convert the value of the current DateTime object to Coordinated Universal Time (UTC).
Syntax
Following is the syntax −
public DateTime ToUniversalTime ();
Example
Let us now see an example to implement the DateTime.ToUniversalTime() method −
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 12, 11, 7, 11, 25); Console.WriteLine("Date = {0}", d); DateTime res = d.ToUniversalTime(); Console.WriteLine("String representation = {0}", res); } }
Output
This will produce the following output −
Date = 11/11/2019 7:11:25 AM String representation = 11/11/2019 7:11:25 AM
Example
Let us now see another example to implement the DateTime.ToUniversalTime() method −
using System; public class Demo { public static void Main() { DateTime localDate, universalDate; String str = "11/11/2019 4:10:55"; localDate = DateTime.Parse(str); universalDate = localDate.ToUniversalTime(); Console.WriteLine("Local time = {0} ", localDate); Console.WriteLine("Universal time = {0} ", universalDate); } }
Output
This will produce the following output −
Local time = 11/11/2019 4:10:55 AM Universal time = 11/11/2019 4:10:55 AM
Advertisements