使用 C# 正則表示式驗證日期格式


使用 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

更新於: 2020 年 6 月 22 日

逾 15,000 次訪問

開啟您的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.