ใช้วิธี DateTime.TryParseExact ใน C # สำหรับการตรวจสอบรูปแบบวันที่
วิธีการแปลงการแสดงสตริงที่ระบุของวันที่และเวลาให้เทียบเท่ากับ DateTime จะตรวจสอบว่ารูปแบบวันที่ที่ป้อนถูกต้องหรือไม่
ตัวอย่าง
using System;
using System.Globalization;
namespace Demo {
class Program {
static void Main(string[] args) {
DateTime d;
bool chValidity = DateTime.TryParseExact(
"08/14/2018",
"MM/dd/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out d);
Console.WriteLine(chValidity);
}
}
} ผลลัพธ์
True