VBScript CDate 函數

定義和用法

CDate 函數可把一個合法的日期和時間表達式轉換為 Date 類型,并返回結果。

提示:請使用 IsDate 函數來判斷 date 是否可被轉換為日期或時間。

注釋:IsDate 函數使用本地設置來檢測字符串是否可被轉換為日期。

語法

CDate(date)
參數 描述
date 必需的。任何有效的日期表達式。(比如 Date() 或者 Now())

實例

例子 1

d="April 22, 2001"
if IsDate(d) then
  document.write(CDate(d))
end if

輸出:

2/22/01

例子 2

d=#2/22/01#
if IsDate(d) then
  document.write(CDate(d))
end if

輸出:

2/22/01

例子 3

d="3:18:40 AM"
if IsDate(d) then
  document.write(CDate(d))
end if

輸出:

3:18:40 AM