Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด DateTime.SpecifyKind() ใน C#

เมธอด DateTime.SpecifyKind() ใน C# ใช้สร้างออบเจ็กต์ DateTime ใหม่ที่มีจำนวนทิกเท่ากับ DateTime ที่ระบุ แต่ถูกกำหนดเป็นเวลาท้องถิ่น (Local), เวลาสากล (UTC) หรือไม่ระบุ ตามค่า DateTimeKind ที่ระบุ

ไวยากรณ์

public static DateTime SpecifyKind (DateTime d, DateTimeKind kind);

พารามิเตอร์ d คือ DateTime ส่วน kind คือค่าการนับที่ระบุว่าออบเจ็กต์ใหม่แสดงเวลาท้องถิ่น UTC หรือไม่ระบุ

ตัวอย่าง

ตัวอย่างที่ 1: กำหนด Kind เป็น Local

using System; 
using System.Globalization; 
public class Demo { 
    public static void Main() { 
        DateTime d = new DateTime(2019, 10, 11, 11, 10, 42); 
        DateTime res = DateTime.SpecifyKind(d, DateTimeKind.Local); 
        Console.WriteLine("Kind = " + res.Kind); 
    } 
}

ผลลัพธ์

Kind = Local

ตัวอย่างเพิ่มเติม

ตัวอย่างที่ 2: กำหนด Kind เป็น Utc

using System; 
public class Demo { 
    public static void Main() { 
        DateTime d = new DateTime(2019, 11, 25, 10, 20, 35); 
        DateTime res = DateTime.SpecifyKind(d, DateTimeKind.Utc); 
        Console.WriteLine("Kind = " + res.Kind); 
    } 
}

ผลลัพธ์

Kind = Utc