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

เมธอด Convert.ToDateTime ใน C#

เมธอด Convert.ToDateTime แปลงค่าที่ให้มาเป็นค่า DateTime

นี่คือสตริงของฉัน:

string myDateString;
myDateString = "09/09/2018";

ตอนนี้ ให้แปลงโดยใช้เมธอด Convert.ToDateTime():

DateTime result; result = Convert.ToDateTime(myDateString);

นี่คือตัวอย่างที่สมบูรณ์

ตัวอย่าง

using System;

public class Demo
{
    public static void Main()
    {
        string myDateString;
        DateTime result;
        myDateString = "09/09/2018";
        result = Convert.ToDateTime(myDateString);
        Console.WriteLine("'{0}' converted to {1}", myDateString, result);
    }
}

ผลลัพธ์

'09/09/2018' converted to 9/9/2018 12:00:00 AM