SHARE
TWEET

Untitled

nanzfff Dec 14th, 2019 296 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' 使用 Choose() 將星期(Integer)轉為對應文字 Choose跟兩個Function意思
  2. Function getWk2(ByVal w As Integer) As String
  3.     If (w > 7) Or (w < 1) Then
  4.         getWk2 = ""
  5.     Else
  6.         getWk2 = Choose(w, "週日", "週一", "週二", "週三", "週四", "週五", "週六")
  7.     End If
  8. End Function
  9.  
  10. Function getDateTime1() As String
  11.     getDateTime1 = Format(Now(), "yyyy-MM-dd-aaa, hh:mm:ss")
  12. End Function
  13.  
  14. Function getDateTime2() As String
  15.     ' Year(), Month(), Day(), Hour(), Minute(), Second() 回傳值為 Integer
  16.    Dim y: y = Year(Now())
  17.     Dim m: m = Month(Now())
  18.     Dim d: d = Day(Now())
  19.     Dim h: h = Hour(Now())
  20.     Dim mn: mn = Minute(Now())
  21.     Dim s: s = Second(Now())
  22.     Dim w: w = Weekday(Now())
  23.    
  24.     ' 根據數值大小判斷是否要補零
  25.    '     小於指定值時補零. 字串(String)與整數(Integer)做字串合併(&)時,Integer 會轉為 String
  26.    '     大於等於指定值時將 Integer 轉為 String
  27.    y = IIf(y < 1000, "0" & y, CStr(y))
  28.     m = IIf(m < 10, "0" & m, CStr(m))
  29.     d = IIf(d < 10, "0" & d, CStr(d))
  30.     h = IIf(h < 10, "0" & h, CStr(h))
  31.     mn = IIf(mn < 10, "0" & mn, CStr(mn))'"0"這幹嘛用?
  32.    s = IIf(s < 10, "0" & s, CStr(s))
  33.     w = getWk2(w)
  34.    
  35.     getDateTime2 = y & "-" & m & "-" & d & "-" & w & ", " & h & ":" & mn & ":" & s'這段是甚麼意思?
  36. End Function
  37.  
  38. Function getDateTime3() As String
  39.     ' Year(), Month(), Day(), Hour(), Minute(), Second() 回傳值為 Integer
  40.    ' 預先將 Integer 轉為 String
  41.    Dim y: y = CStr(Year(Now()))
  42.     Dim m: m = CStr(Month(Now()))
  43.     Dim d: d = CStr(Day(Now()))
  44.     Dim h: h = CStr(Hour(Now()))
  45.     Dim mn: mn = CStr(Minute(Now()))
  46.     Dim s: s = CStr(Second(Now()))
  47.     Dim w: w = getWk1(Weekday(Now()))
  48.    
  49.     ' String() 為產生重複文字的函數 (Strings 模組)
  50.    y = String(4 - Len(y), "0") + y
  51.     m = String(2 - Len(m), "0") + m
  52.     d = String(2 - Len(d), "0") + d
  53.     h = String(2 - Len(h), "0") + h
  54.     mn = String(2 - Len(mn), "0") + mn'怎麼判斷?-?補0?
  55.    s = String(2 - Len(s), "0") + s
  56.    
  57.     getDateTime3 = y & "-" & m & "-" & d & "-" & w & ", " & h & ":" & mn & ":" & s
  58. End Function
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top