使用 C# Regex 進行日期格式驗證


使用 C# 中的 DateTime.TryParseExact 方法進行日期格式驗證。

該方法將給定的日期和時間的字串表示形式轉換為其 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

更新於: 22-6 月-2020

14K+ 瀏覽次數

開啟你的職業生涯

完成課程即可獲得認證

開始使用
廣告
© . All rights reserved.