DateTime.SpecifyKind() Method in C#

This method is used to create a new DateTime object which 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.

Syntax: public static DateTime SpecifyKind (DateTime value, DateTimeKind kind);

Parameters:
value: It is the date and time.
kind: It is one of the enumeration values which indicates whether the new object represents local time, UTC, or neither.

Return Value: This method returns a new object that has the same number of ticks as the object represented by the value parameter and the DateTimeKind value specified by the kind parameter.

Below programs illustrate the use of DateTime.SpecifyKind(DateTime, DateTimeKind) Method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.SpecifyKind(DateTime,
// DateTimeKind) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating object of DateTime
        DateTime date = new DateTime(2005, 5,
                                6, 14, 34, 42);
                                  
        Console.WriteLine("Kind Before Using Method: "
                                          +date.Kind);
  
        // getting DateTime of same DateTime 
        // instance using SpecifyKind() method
        DateTime value = DateTime.SpecifyKind(date,
                            DateTimeKind.Local);
                              
        Console.WriteLine("Kind After Using Method: " +
                                            value.Kind);
  
        Console.WriteLine("DateTime is {0}",
                                    value);
    }
}

chevron_right


Output:

Kind Before Using Method: Unspecified
Kind After Using Method: Local
DateTime is 05/06/2005 14:34:42

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.SpecifyKind(DateTime,
// DateTimeKind) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating object of DateTime
        DateTime date = new DateTime(1970, 1,
                                1, 4, 0, 15);
  
        // getting DateTime of same DateTime 
        // instance using SpecifyKind() method
        DateTime value = DateTime.SpecifyKind(date,
                               DateTimeKind.Local);
  
        Console.WriteLine("DateTime is {0}", value);
    }
}

chevron_right


Output:

DateTime is 01/01/1970 04:00:15

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.