Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTime.SpecifyKind() Method in C#
The DateTime.SpecifyKind() method in C# is used to create a new DateTime object that has the same number of ticks as the specified DateTime but is designated as either local time, Coordinated Universal Time (UTC), or neither, as indicated by the specified DateTimeKind value.
This method is particularly useful when you have a DateTime value but need to explicitly specify its time zone context without changing the actual time value. The original DateTime remains unchanged − a new instance is returned with the specified Kind property.
Syntax
Following is the syntax for the DateTime.SpecifyKind() method −
public static DateTime SpecifyKind(DateTime d, DateTimeKind kind);
Parameters
-
d − The
DateTimeobject whoseKindproperty is to be modified. -
kind − One of the
DateTimeKindenumeration values that indicates whether the new object represents local time, UTC, or neither.
Return Value
Returns a new DateTime object that has the same number of ticks as the input DateTime but with the specified DateTimeKind value.
Using SpecifyKind with Local Time
Example
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 10, 11, 11, 10, 42);
Console.WriteLine("Original Kind: " + d.Kind);
DateTime res = DateTime.SpecifyKind(d, DateTimeKind.Local);
Console.WriteLine("New Kind: " + res.Kind);
Console.WriteLine("Same time value: " + (d == res));
}
}
The output of the above code is −
Original Kind: Unspecified New Kind: Local Same time value: True
Using SpecifyKind with UTC
Example
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 11, 25, 10, 20, 35);
DateTime utcTime = DateTime.SpecifyKind(d, DateTimeKind.Utc);
Console.WriteLine("Original: " + d + " (Kind: " + d.Kind + ")");
Console.WriteLine("UTC: " + utcTime + " (Kind: " + utcTime.Kind + ")");
Console.WriteLine("Ticks are equal: " + (d.Ticks == utcTime.Ticks));
}
}
The output of the above code is −
Original: 11/25/2019 10:20:35 AM (Kind: Unspecified) UTC: 11/25/2019 10:20:35 AM (Kind: Utc) Ticks are equal: True
DateTimeKind Enumeration Values
| DateTimeKind Value | Description |
|---|---|
| Unspecified | The time is not specified as either local time or UTC. |
| Local | The time is specified as a local time. |
| Utc | The time is specified as UTC (Coordinated Universal Time). |
Common Use Cases
The SpecifyKind() method is commonly used when:
-
Converting between time zones and you need to explicitly specify the source time zone context.
-
Working with database timestamps that don't include time zone information.
-
Processing date/time data from external sources where the Kind property needs to be set correctly.
Conclusion
The DateTime.SpecifyKind() method creates a new DateTime object with the same time value but a different Kind property. This method is essential for correctly handling time zone contexts in applications that work with different time representations without modifying the actual time value.
