Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for date2iso.vbs

(view source code of date2iso.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim blnDate, blnTime
  4. Dim dtmDate
  5. Dim intDay, intFormat, intHour, intMin, intMonth, intSec, intUTC, intValid, intYear
  6. Dim strISO
  7.  
  8. With WScript.Arguments
  9. 	' Check command line arguments
  10. 	If .Unnamed.Count = 0 Then dtmDate = Now
  11. 	If .Unnamed.Count > 0 Then dtmDate = .Unnamed(0)
  12. 	If .Unnamed.Count > 1 Then dtmDate = dtmDate & " " & .Unnamed(1)
  13. 	If .Unnamed.Count > 2 Then dtmDate = dtmDate & " " & .Unnamed(2)
  14. 	If .Unnamed.Count > 3 Then Syntax
  15. 	On Error Resume Next
  16. 	dtmDate = CDate( dtmDate )
  17. 	If Err Then
  18. 		On Error Goto 0
  19. 		Syntax
  20. 	End If
  21. 	On Error Goto 0
  22. 	If Not IsDate( dtmDate ) Then Syntax
  23. 	intValid = 0
  24. 	blnDate  = True
  25. 	blnTime  = True
  26. 	If .Named.Exists( "D" ) Then
  27. 		blnDate  = True
  28. 		blnTime  = False
  29. 		intValid = intValid + 1
  30. 	End If
  31. 	If .Named.Exists( "T" ) Then
  32. 		blnDate  = False
  33. 		blnTime  = True
  34. 		intValid = intValid + 1
  35. 	End If
  36. 	If intValid <> .Named.Count Then Syntax
  37. 	If intValid > 1 Then Syntax
  38. End With
  39.  
  40. ' Format the output string
  41. intYear  = DatePartLZ( "yyyy", dtmDate )
  42. intMonth = DatePartLZ( "m", dtmDate )
  43. intDay   = DatePartLZ( "d", dtmDate )
  44. intHour  = DatePartLZ( "h", dtmDate )
  45. intMin   = DatePartLZ( "n", dtmDate )
  46. intSec   = DatePartLZ( "s", dtmDate )
  47. If blnDate Then strISO = intYear & "-" & intMonth & "-" & intDay
  48. If blnTime Then strISO = strISO & " " & intHour & ":" & intMin & ":" & intSec
  49. ' Display the result
  50. WScript.Echo Trim( strISO )
  51.  
  52.  
  53. Function DatePartLZ( myInterval, myDate )
  54. 	' Add a leading zero to the DatePart() if necessary
  55. 	Dim strDatePart
  56. 	strDatePart = DatePart( myInterval, myDate )
  57. 	If Len( strDatePart ) < 2 Then strDatePart = "0" & strDatePart
  58. 	DatePartLZ = strDatePart
  59. End Function
  60.  
  61.  
  62. Sub Syntax
  63. 	WScript.Echo vbcrlf _
  64. 	           & "Date2ISO.vbs,  Version 1.02" _
  65. 	           & vbCrLf _
  66. 	           & "Convert any date/time to ISO date/time" _
  67. 	           & vbCrLf & vbCrLf _
  68. 	           & "Usage:  CSCRIPT.EXE  //NoLogo  Date2ISO.vbs  date  [ time ]  [ /D | /T ]" _
  69. 	           & vbCrLf & vbCrLf _
  70. 	           & "Where:  ""date""   is the date to convert (default: current date/time)" _
  71. 	           & vbCrLf _
  72. 	           & "        ""time""   is the optional time to convert" _
  73. 	           & vbCrLf _
  74. 	           & "        /D       return date only (default: both date and time)" _
  75. 	           & vbCrLf _
  76. 	           & "        /T       return time only (/D and /T are mutually exclusive)" _
  77. 	           & vbCrLf & vbCrLf _
  78. 	           & "Note:   If the specified date is ambiguous, the current user's date" _
  79. 	           & vbCrLf _
  80. 	           & "        and time format is assumed." _
  81. 	           & vbCrLf & vbCrLf _
  82. 	           & "Written by Rob van der Woude" _
  83. 	           & vbCrLf _
  84. 	           & "http://www.robvanderwoude.com"
  85. 	WScript.Quit 1
  86. End Sub
  87.  

page last uploaded: 2017-04-06, 13:33