I sure would appreciate some help. I was able to hack away at getting my time formatted as 8:53 AM from a value such as this 7/31/2020 08:53:20 AM with the following.
replace(replace(formatdatetime(formatdatetime(myRS("dateTime"),4)),":00 AM"," AM"),":00 PM"," PM")
But I would think there is a more efficient approach no?
<%#LANGUAGE="VBScript"%>
<%
' The actual LCID.
Response.LCID = 2057
Response.CodePage = 65001
Function AM_PM_Time(TheTime, ZeroPad)
Dim actual_LCID, time_formatted, time_split_1, time_split_2
' Make sure the the date or time being passed is valid.
If NOT isDate(TheTime) Then Exit Function
' Make a note of the actual LCID.
actual_LCID = Response.LCID
' Change the LCID to 1033 so time is formatted as 00:00:00 AM/PM.
' If your LCID is already 1033 you can delete the LCID code.
Response.LCID = 1033
' Format the current date / time.
time_formatted = FormatDateTime(TheTime,3)
' Split the time stamp.
time_split_1 = Split(time_formatted,":")
' Split the AM / PM.
time_split_2 = Split(time_formatted," ")
' Revert the LCID back to its original value.
Response.LCID = actual_LCID
' [Optional]
' Zero-pad the first number of the timestamp, for example: "07:45 PM" rather than "7:45 PM"
If ZeroPad Then
If Len(time_split_1(0)) = 1 Then time_split_1(0) = cStr("0" & time_split_1(0))
End If
' Output the newly formatted time.
AM_PM_Time = time_split_1(0) & ":" & time_split_1(1) & " " & time_split_2(1)
End Function
Response.Write AM_PM_Time(Now(),True) & "<br>"
Response.Write AM_PM_Time("2020-08-17 09:00:00",False) & "<br>"
Response.Write AM_PM_Time("14:55:00",True)
%>
Example output:
07:45 PM9:00 AM02:55 PM
Set speech = CreateObject("sapi.spvoice")
Speech.speak "Here's one way it sets the locale to New Zealand which is 12 hours - it does NOT set time zone"
SetLocale("en-nz")
speech.speak now()
speech.speak "Here's another way" 'if locale is 12 hours it does nothing
Do
If Hour(Now) < 12 then
Var = Hour(Now) & " AM"
else
Var = Hour(Now) - 12 & " PM"
End If
speech.Speak Var & " and " & Minute(Now) & " minutes and " & Second(Now) & " seconds"
wscript.sleep 5
Loop
As you can see it speaks the comments.
Related
I have last boot time from WMI and it looks as '20141103113859.220250+060'. i want to convert it to number of days and time from the current time.
is it possible?
From Help
Use the SWbemDateTime object to convert these to regular dates and times.
Windows 2000/NT and Windows 98/95: SWbemDateTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.
Set dtmInstallDate = CreateObject( _
"WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set objOS = objWMIService.ExecQuery( _
"Select * from Win32_OperatingSystem")
For Each strOS in objOS
dtmInstallDate.Value = strOS.InstallDate
Wscript.Echo dtmInstallDate.GetVarDate
Next
To get help.
http://msdn.microsoft.com/en-us/windows/hardware/hh852363
Install the Windows SDK but just choose the documentation.
Next simple function should work for any argument in valid CIM_DATETIME format.
Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = ( Left(dtmDate, 4) _
& "/" & Mid(dtmDate, 5, 2) _
& "/" & Mid(dtmDate, 7, 2) _
& " " & Mid(dtmDate, 9, 2) _
& ":" & Mid(dtmDate,11, 2) _
& ":" & Mid(dtmDate,13, 2))
End Function
An example:
InstallDate (wmi): 20141205231553.000000+060
InstallDate: 2014/12/05 23:15:53
However, a wmi query could return Null, e.g. VarType(dtmDate)=1 for a particular instance of a date; in next script is the function modified:
option explicit
Dim strWmiDate
strWmiDate = "20141103113859.220250+060"
Wscript.Echo strWmiDate _
& vbNewLine & WMIDateStringToDate(strWmiDate) _
& vbNewLine & DateDiff("d", WMIDateStringToDate(strWmiDate), Now) _
& vbNewLine _
& vbNewLine & WMIDateStringToDate(Null) _
& vbNewLine & DateDiff("d", WMIDateStringToDate(Null), Now)
Function WMIDateStringToDate(byVal dtmDate)
If VarType(dtmDate)=1 Then
WMIDateStringToDate = FormatDateTime( Now) 'change to whatever you want
Else
'
' to keep script locale independent:
' returns ANSI (ISO 8601) datetime format (24 h)
'
' yyyy-mm-dd HH:MM:SS
'
WMIDateStringToDate = Left(dtmDate, 4) _
& "-" & Mid(dtmDate, 5, 2) _
& "-" & Mid(dtmDate, 7, 2) _
& " " & Mid(dtmDate, 9, 2) _
& ":" & Mid(dtmDate,11, 2) _
& ":" & Mid(dtmDate,13, 2)
End If
End Function
Output:
==>cscript 29535638.vbs
20141103113859.220250+060
2014-11-03 11:38:59
157
09.04.2015 15:36:38
0
#Serenity has given this same answer while i was writting, but ...
Option Explicit
WScript.Echo getLastBootUpTime()
WScript.Echo WMIDate2Date( "20141103113859.220250+060" )
WScript.Echo GetElapsedTime( getLastBootUpTime(), Now )
Function WMIDate2Date( ByVal WMIDate )
With WScript.CreateObject("WbemScripting.SWbemDateTime")
.Value = WMIDate
WMIDate2Date = .GetVarDate(False)
End With
End Function
Function getLastBootUpTime()
Dim oOS
For Each oOS In GetObject( "winmgmts:\\.\root\cimv2").ExecQuery("Select LastBootUpTime from Win32_OperatingSystem")
getLastBootUpTime = WMIDate2Date(oOS.LastBootUpTime)
Next
End Function
Function GetElapsedTime( ByVal Date1, ByVal Date2 )
Dim seconds, aLabels, aValues, aDividers, i
aLabels = Array( " days, ", ":", ":", "" )
aDividers = Array( 86400, 3600, 60, 1 )
aValues = Array( 0, 0, 0, 0 )
i = 0
seconds = Abs( DateDiff( "s", Date1, Date2 ))
Do While seconds > 0
aValues(i) = Fix( seconds / aDividers(i) )
seconds = seconds - aValues(i) * aDividers(i)
aValues(i) = CStr(aValues(i)) & aLabels(i)
i=i+1
Loop
GetElapsedTime = Join(aValues, "")
End Function
You won't get around splitting the WMI date string to make it to a date string that VBScript understands. Try this:
<%
wmiDate = "20141103113859.220250+060"
' note i am using date format: [m/d/Y H:m:s]
' if you prefer other format, i.e. [d.m.Y H:m:s] switch mid offsets
fromDate = Mid(wmiDate,5,2) & "/" & Mid(wmiDate,7,2) & "/" & Left(wmiDate,4)
fromTime = Mid(wmiDate,9,2) & ":" & Mid(wmiDate,11,2) & ":" & Mid(wmiDate,13,2)
toDate = Date & " " & Time
response.write(DateDiff("d",fromDate & " " & fromTime,toDate) & " Days<br />")
response.write(DateDiff("h",Date & " " & fromTime,toDate) & " Hours<br />")
%>
It uses Mid()and Left()functions to split WMI date into the needed parts for VBScript. Then the DateDiff() function will deliver the interval difference first for d= days and then for h= hours. You will notice when calculating hours i just used the time part of the WMI string, since we already calculated days difference, we only want hours left over.
Interesting article explaining VBScript Date and Time (Iso Formats)
As a comment was so kindly remarking the date format i used and the result of the hour calculation, i added a comment line explaining the date format i used (i used m/d/Y H:m:s but depending on your local, you might prefer d.m.Y H:m:s then you need to swap the Mid() offsets to get the right order). I also appended the current Time to the toDate and in the hour calculation prepended the current Date to calculate the correct time difference.
Trying to Achieve
I fixed a date on my code say 31-01-2019. Then everyday I will execute my code but only on 28-02-2019/29-02-2020, 31-03-2019, 30-04-2019... I wish to execute the code. It is something like monthly generation. In addition, if the fixed date is 30-01-2019, I wish to execute the code on 28-02-2019/29-02-2020, 30-03-2019, 30-04-2019...
For example
What I have done
I have followed the question VBScript DateDiff month, and have tried out the following code but it is not working.
If I were to have a date say 31-Jan-2010 by DateAdd
endFeb = DateAdd("m",1,"31-Jan-10")
endMar = DateAdd("m",1,endFeb)
endApr = DateAdd("m",1,endMar)
The result
endFeb: 28/02/2010
endMar: 28/03/2010
endApr: 28/04/2010
What I want is
endFeb: 28/02/2010
endMar: 31/03/2010
endApr: 30/04/2010
Code
sFixedDate = "2019-01-31" '==== Fixed
sProcessDate = "2019-02-28" '==== Changes daily
d1 = CDate(sFixedDate)
d2 = CDate(sProcessDate)
diff = DateDiff("m", d1, d2)
If request("btnProcess") <> "" Then
If diff Mod 1 = 0 Then '=== Not as simple as I thought
'=== Trying to do monthly GENERATION.
'===Excecute the CODE
End If
End If
Basically, you want to run something on the last day of each month. Meaning that the day after would be a different month, so you could do something like this for calculating the last day of the next month:
today = Date
tomorrow = today + 1
If request("btnProcess") <> "" Then
If Month(today) <> Month(tomorrow) Then
endNextMonth = DateAdd("m", 1, tomorrow) - 1
End If
End If
To get the last day for any given month adjust the number of months to add to tomorrow's date.
The above assumes that you're doing the calculation on the last day of a month. If you want to calculate the last day of any given month on any day of a month please see Ekkehard Horner's answer.
Use DateSerial:
For m = 1 To 13
d1 = DateSerial(2019, m, 1) ' First day of month is easy
d2 = DateAdd("d", d1, -1) ' Last day of previous month is just 1 day before
WScript.Echo m, d1, d2
Next
cscript lom.vbs
1 01.01.2019 31.12.2018
2 01.02.2019 31.01.2019
3 01.03.2019 28.02.2019
4 01.04.2019 31.03.2019
5 01.05.2019 30.04.2019
6 01.06.2019 31.05.2019
7 01.07.2019 30.06.2019
8 01.08.2019 31.07.2019
9 01.09.2019 31.08.2019
10 01.10.2019 30.09.2019
11 01.11.2019 31.10.2019
12 01.12.2019 30.11.2019
13 01.01.2020 31.12.2019
It seems like for a given start date, you want to calculate x months into the future what that new date is, and if the start date as a day that is greater than the future month, to give the last day of the month instead.
Function CalculateFutureDate(startDate, monthsInFuture)
' Assumes startDate is in the past
Dim dtRepeatDate
Dim dtNewDate
If (IsDate(startDate)) Then
dtRepeatDate = CDate(startDate)
' months between now and Start Date
Dim intMonthsToAdd
Dim dtCurrentDate
dtCurrentDate = Now()
intMonthsToAdd = DateDiff("m", startDate, dtCurrentDate)
If intMonthsToAdd > 0 And Day(startDate) < Day(dtCurrentDate) Then
intMonthsToAdd = intMonthsToAdd - 1
End If
' Add the future months to the month offset
intMonthsToAdd = intMonthsToAdd + monthsInFuture
' Now calculate future date
dtNewDate = DateAdd("m", intMonthsToAdd, dtRepeatDate)
CalculateFutureDate = dtNewDate
End If
End Function
And then you can do something like:
CalculateFutureDate(CDate("2019-01-31"), intFutureMonths)
This will output:
?CalculateFutureDate(CDate("2019-01-31"), 1)
2/28/2019
?CalculateFutureDate(CDate("2019-01-31"), 2)
3/31/2019
?CalculateFutureDate(CDate("2019-01-31"), 3)
4/30/2019
dtLoan = CDate("2019-01-30")
dtProcess = CDate ("2020-02-28")
'dtLoan = CDate("2019-01-31")
'dtProcess = CDate ("2020-02-29")
'dtLoan = CDate("2019-02-28")
'dtProcess = CDate ("2020-02-29")
if LastDateOfMonth(dtLoan) = dtLoan AND dtProcess = LastDateOfMonth(dtProcess) then
response.write " this mean that the Loan date is end of the month, say 31 Jan, 28, 29 of Feb, 31 Feb "
response.write " and Process Date is also end of the month " & "<br>"
response.write " **** End of the month Loan Date : " & dtLoan & "<br>"
response.write " **** End of the month Process Date : " & dtProcess & "<br>"
elseif LastDateOfMonth(dtLoan) <> dtLoan AND dtProcess <> LastDateOfMonth(dtProcess) then
daysFromEndOfLoanMth = DateDiff("d",LastDateOfMonth(dtLoan),dtLoan)
response.write " How many days from end of Loan month: " & daysFromEndOfLoanMth & "<br>"
daysFromEndOfProcessMth = DateAdd("d",daysFromEndOfLoanMth,LastDateOfMonth(dtProcess))
response.write " From end of the month Add " & daysFromEndOfLoanMth & " Days = " & daysFromEndOfProcessMth & "<br>"
response.write " The date of process : " & dtProcess & "<br>"
dtShouldProcess = day(dtLoan) & "/" & Month(dtProcess) & "/" & Year(dtProcess)
if isDate(dtShouldProcess) then
dtShouldProcess=CDate(dtShouldProcess)
else
dtShouldProcess=daysFromEndOfProcessMth
end if
response.write " ** The date of should Process : ** " & dtShouldProcess & "<br>"
if dtProcess = dtShouldProcess then
'if dtProcess = daysFromEndOfProcessMth then
response.write " **** Loan Date : " & dtLoan & "<br>"
response.write " **** Process Date : " & dtProcess & "<br>"
end if
'daysFromEndOfProcessMth = DateDiff("d",LastDateOfMonth(dtProcess1),dtProcess1)
'response.write " How many days from Process Date end of the month: " & daysFromEndOfProcessMth & "<br>"
end if
When you type the minute to be the same as the current minute (Ex. you type in 20 and its currently 1:20) I want it to end the script. Instead it gives me
Invalid procedure call or argument error.
Dim Ho, Mi, Se, RH, RM, RS
bOuterLoop = True
Do While bOuterLoop
reminder = InputBox(vbNewLine & "Enter a reminder for today," & " " &
WeekdayName(Weekday(Now())) & " " & MonthName (Month(Now)) & " " & (Day(Now)) & ", " & (Year(Now)) & "!", "Set a Reminder")
If IsEmpty(reminder) Then
WScript.Quit
End If
If reminder="" then
reminder="REMINDER FOR YOU"
end if
Ho = InputBox(vbNewLine & "At what hour would you like to be reminded?(24H)", "Time")
If IsEmpty(Ho) Then
WScript.Quit
End If
if Ho = "" then
WScript.Quit
End If
Mi = InputBox(vbNewLine & "At what minute in the hour do you want to be
reminded?", "Time")
If IsEmpty(Mi) Then
WScript.Quit
End If
if Mi = "" then
WScript.Quit
End If
RH = Hour(Now)
RM = Minute(Now)
RS = Second(Now)
if Mi = RM then
a=msgbox(reminder, 0, "")
wscript.quit
end if
Ho = Ho - RH
Se = RS
if RM > Mi then
Mi = Mi + 60
end if
Mi = Mi - RM
Do
WScript.Sleep (3600000 * Ho) + ((60000 * Mi) - (1000 * Se))
answer = MsgBox(reminder & " " & vbNewLine & vbNewLine & "Would you like to snooze the reminder? ", 4+64, "Reminder")
If answer = vbNo Then
WScript.Quit
bOuterLoop = False
Exit Do
End If
Loop
Loop
Please explain what I'm doing wrong.
I am not sure about the logic which you are using here but I think the 'If' condition comparing the values of variables 'Mi' and 'RM' is returning False.
Suppose the current time is 1:20.
In your code, the variable Mi stores the input taken from the user which is of type vbString. Suppose the user enters 20. So, your variable looks something like this:
Mi="20"
The variable RM stores Minute(now) which is of type vbInteger. So, it would look something like this:
RM=20
Now when you compare these two variables, they are not equal. Hence, the If condition returns False and your script does not end.
You can make the following change in your code:
If Cint(Mi) = RM then
a=msgbox(reminder, 0, "")
wscript.quit
End If
I have last boot time from WMI and it looks as '20141103113859.220250+060'. i want to convert it to number of days and time from the current time.
is it possible?
From Help
Use the SWbemDateTime object to convert these to regular dates and times.
Windows 2000/NT and Windows 98/95: SWbemDateTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.
Set dtmInstallDate = CreateObject( _
"WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set objOS = objWMIService.ExecQuery( _
"Select * from Win32_OperatingSystem")
For Each strOS in objOS
dtmInstallDate.Value = strOS.InstallDate
Wscript.Echo dtmInstallDate.GetVarDate
Next
To get help.
http://msdn.microsoft.com/en-us/windows/hardware/hh852363
Install the Windows SDK but just choose the documentation.
Next simple function should work for any argument in valid CIM_DATETIME format.
Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = ( Left(dtmDate, 4) _
& "/" & Mid(dtmDate, 5, 2) _
& "/" & Mid(dtmDate, 7, 2) _
& " " & Mid(dtmDate, 9, 2) _
& ":" & Mid(dtmDate,11, 2) _
& ":" & Mid(dtmDate,13, 2))
End Function
An example:
InstallDate (wmi): 20141205231553.000000+060
InstallDate: 2014/12/05 23:15:53
However, a wmi query could return Null, e.g. VarType(dtmDate)=1 for a particular instance of a date; in next script is the function modified:
option explicit
Dim strWmiDate
strWmiDate = "20141103113859.220250+060"
Wscript.Echo strWmiDate _
& vbNewLine & WMIDateStringToDate(strWmiDate) _
& vbNewLine & DateDiff("d", WMIDateStringToDate(strWmiDate), Now) _
& vbNewLine _
& vbNewLine & WMIDateStringToDate(Null) _
& vbNewLine & DateDiff("d", WMIDateStringToDate(Null), Now)
Function WMIDateStringToDate(byVal dtmDate)
If VarType(dtmDate)=1 Then
WMIDateStringToDate = FormatDateTime( Now) 'change to whatever you want
Else
'
' to keep script locale independent:
' returns ANSI (ISO 8601) datetime format (24 h)
'
' yyyy-mm-dd HH:MM:SS
'
WMIDateStringToDate = Left(dtmDate, 4) _
& "-" & Mid(dtmDate, 5, 2) _
& "-" & Mid(dtmDate, 7, 2) _
& " " & Mid(dtmDate, 9, 2) _
& ":" & Mid(dtmDate,11, 2) _
& ":" & Mid(dtmDate,13, 2)
End If
End Function
Output:
==>cscript 29535638.vbs
20141103113859.220250+060
2014-11-03 11:38:59
157
09.04.2015 15:36:38
0
#Serenity has given this same answer while i was writting, but ...
Option Explicit
WScript.Echo getLastBootUpTime()
WScript.Echo WMIDate2Date( "20141103113859.220250+060" )
WScript.Echo GetElapsedTime( getLastBootUpTime(), Now )
Function WMIDate2Date( ByVal WMIDate )
With WScript.CreateObject("WbemScripting.SWbemDateTime")
.Value = WMIDate
WMIDate2Date = .GetVarDate(False)
End With
End Function
Function getLastBootUpTime()
Dim oOS
For Each oOS In GetObject( "winmgmts:\\.\root\cimv2").ExecQuery("Select LastBootUpTime from Win32_OperatingSystem")
getLastBootUpTime = WMIDate2Date(oOS.LastBootUpTime)
Next
End Function
Function GetElapsedTime( ByVal Date1, ByVal Date2 )
Dim seconds, aLabels, aValues, aDividers, i
aLabels = Array( " days, ", ":", ":", "" )
aDividers = Array( 86400, 3600, 60, 1 )
aValues = Array( 0, 0, 0, 0 )
i = 0
seconds = Abs( DateDiff( "s", Date1, Date2 ))
Do While seconds > 0
aValues(i) = Fix( seconds / aDividers(i) )
seconds = seconds - aValues(i) * aDividers(i)
aValues(i) = CStr(aValues(i)) & aLabels(i)
i=i+1
Loop
GetElapsedTime = Join(aValues, "")
End Function
You won't get around splitting the WMI date string to make it to a date string that VBScript understands. Try this:
<%
wmiDate = "20141103113859.220250+060"
' note i am using date format: [m/d/Y H:m:s]
' if you prefer other format, i.e. [d.m.Y H:m:s] switch mid offsets
fromDate = Mid(wmiDate,5,2) & "/" & Mid(wmiDate,7,2) & "/" & Left(wmiDate,4)
fromTime = Mid(wmiDate,9,2) & ":" & Mid(wmiDate,11,2) & ":" & Mid(wmiDate,13,2)
toDate = Date & " " & Time
response.write(DateDiff("d",fromDate & " " & fromTime,toDate) & " Days<br />")
response.write(DateDiff("h",Date & " " & fromTime,toDate) & " Hours<br />")
%>
It uses Mid()and Left()functions to split WMI date into the needed parts for VBScript. Then the DateDiff() function will deliver the interval difference first for d= days and then for h= hours. You will notice when calculating hours i just used the time part of the WMI string, since we already calculated days difference, we only want hours left over.
Interesting article explaining VBScript Date and Time (Iso Formats)
As a comment was so kindly remarking the date format i used and the result of the hour calculation, i added a comment line explaining the date format i used (i used m/d/Y H:m:s but depending on your local, you might prefer d.m.Y H:m:s then you need to swap the Mid() offsets to get the right order). I also appended the current Time to the toDate and in the hour calculation prepended the current Date to calculate the correct time difference.
This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 4 years ago.
My issue is when I retrieve date back it gives me in this format:
lastseenstatus=rsprefobj("lastseentstamp")
19-07-2014 15:31:32
I want it in 7/19/2014 3:31:32 PM format with AM/PM intact.
Please help..
First and foremost you need to determine the data type of rsprefobj("lastseentstamp"):
MsgBox TypeName(rsprefobj("lastseentstamp"))
If it's a string, you need to convert it to a datetime value first:
lastseenstatus = CDate(rsprefobj("lastseentstamp"))
If you want the date formatted according to the system's regional settings, use the FormatDateTime() function as #John suggested:
MsgBox FormatDateTime(lastseenstatus)
If you need a distinct date format regardless of the system's regional settings you have to either build the formatted string yourself:
Function LPad(v) : LPad = Right("00" & v, 2) : End Function
Function FormatDate(d)
formattedDate = Month(d) & "/" & LPad(Day(d)) & "/" & Year(d) & " " & _
((Hour(d) + 23) Mod 12 + 1) & ":" & LPad(Minute(d)) & ":" & _
LPad(Second(d))
If Hour(d) < 12 Then
formattedDate = formattedDate & " AM"
Else
formattedDate = formattedDate & " PM"
End If
FormatDate = formattedDate
End Function
MsgBox FormatDate(lastseenstatus)
or use the .Net StringBuilder class:
Set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat "{0:M\/dd\/yyyy h:mm:ss tt}", lastseenstatus
MsgBox sb.ToString()
In my tests I wasn't able to get the tt format specifier to work, though, so you may have to resort to something like this:
Set sb = CreateObject("System.Text.StringBuilder")
If Hour(lastseenstatus) < 12 Then
am_pm = "AM"
Else
am_pm = "PM"
End If
sb.AppendFormat_5 Nothing, "{0:M\/dd\/yyyy h:mm:ss} {1}", _
Array(lastseenstatus, am_pm)
MsgBox sb.ToString()
I'm assuming you are using VBScript and not VB.NET like you have tagged.
Use FormatDateTime(lastseenstatus).
That should give you the format "2/16/2010 1:45:00 PM".