I have these records on my Day Table name tblday
M
T
W
TH
F
S
MW
TTH
WF
Let say if I have this existing schedule:
ScheduleID = 10001
StartTime = 8:30 AM
EndTime = 1:00 PM
Day = M
Room = AVR
Course = BSN
Then If I add this new entry
ScheduleID = 10002
StartTime = 9:00 AM
EndTime = 10:00 AM
Day = MW
Room = AVR
Course = BSN
This should prompt a conflict in schedule because there is already a schedule for monday, then the new entry shouldn't be added. : )
Note: MW means 'Monday' AND 'Wednesday', I used this if they have both the same schedule. Because it would become redundant if add a new schedule for monday and add another for thursday with the same day, time, room and course. Also the same as TTH and MWF I can only detect conflict if it is not a combination of both Days (e.g MTH, TF, ...) I really spend a lot of time regarding this issue. Please I really need your help : (
Heres the code:
Function RoomInUse() As Boolean<br><br>
Dim room As String<br>
Dim day As String<br>
Dim starttime As Date<br>
Dim endtime As Date<br>
Dim mond As String<br>
Set rs = New ADODB.Recordset<br>
With rs<br>
mond = "select * from tblsched where room Like '" & room & "%' and day like '" & room & "' and (starttime <= #" & starttime & "# And " & _<br>
"endtime >= #" & starttime & "#) Or (#" & starttime & "#" & _<br>
"<= starttime And endtime < #" & endtime & "#) Or (#" & _<br>
starttime & "# <= sarttime And starttime < #" & endtime & "#)) " '"<br>
.Open mond, con, 3, 3<br>
End With
If rs.RecordCount >= 1 Then
RoomInUse = True
Else
RoomInUse = False
End If
End Function
Related
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
This question already has answers here:
Find time with millisecond using VBScript
(3 answers)
Closed 5 years ago.
My VBScript has a log file which logs information with current date and time using FormatDateTime Function.
I like to format time up to milliseconds and also in the following format:
MM/DD/YY hh:mm:ss:mss AM/PM
But, unfortunately FormatDateTime doesn't let to format time in this way.
After searching for this, I found this answer and it is about how to use Timer function, So I can't log time to log files using it again and again.
As W3schools states,
The Timer function returns the number of seconds since 12:00 AM.
But I want my log file to log time in above format even before 12:00 AM, So using Timer Function isn't the best option for this.
Please let me know a way to do this specially in log files correctly.
The vbscript function Now() will return the current system date and time in this format - 5/2/2017 9:45:34 AM, however if you need to add milliseconds you can use Timer - Timer math from here
'capture the date and timer together so if the date changes while
'the other code runs the values you are using don't change
t = Timer
dateStr = Date()
temp = Int(t)
milliseconds = Int((t-temp) * 1000)
seconds = temp mod 60
temp = Int(temp/60)
minutes = temp mod 60
hours = Int(temp/60)
label = "AM"
If hours > 12 Then
label = "PM"
hours = hours-12
End If
'format it and add the date
strTime = LeftPad(hours, "0", 2) & ":"
strTime = strTime & LeftPad(minutes, "0", 2) & ":"
strTime = strTime & LeftPad(seconds, "0", 2) & "."
strTime = strTime & LeftPad(milliseconds, "0", 3)
WScript.Echo dateStr & " " & strTime & " " & label
'this function adds characters to a string to meet the desired length
Function LeftPad(str, addThis, howMany)
LeftPad = String(howMany - Len(str), addThis) & str
End Function
Another way to do the same thing, with a bit less code. In this code, we're splitting Now() into an array so we can use it for everything except the milliseconds.:
WScript.Echo PrintTimeStamp()
Function PrintTimeStamp()
nowParts = SPLIT(Now(), " ")
timePart = nowParts(1)
t = Timer
milliseconds = Int((t-Int(t)) * 1000)
PrintTimeStamp = nowParts(0) & " " & LeftPad(nowParts(1), "0", 8) & "." & LeftPad(milliseconds, "0", 3) & " " & nowParts(2)
End Function
Function LeftPad(str, addThis, howMany)
LeftPad = String(howMany - Len(str), addThis) & str
End Function
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.
Does anyone have a simple means in VBScript to get the current time in UTC?
Thanx,
Chris
I use a simple technique
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate (now())
wscript.echo "Local Time: " & dateTime
wscript.echo "UTC Time: " & dateTime.GetVarDate (false)
More info on SWbemDateTime
If you wanted to convert UTC back to local time do this:
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate now(),false REM Where now is the UTC date
wscript.echo cdate(dateTime.GetVarDate (true))
There are lots of examples out there. If you can access the registry this one will work for you:
od = now()
set oShell = CreateObject("WScript.Shell")
atb = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\" &_
"Control\TimeZoneInformation\ActiveTimeBias"
offsetMin = oShell.RegRead(atb)
nd = dateadd("n", offsetMin, od)
Response.Write("Current = " & od & "<br>UTC = " & nd)
From http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-convert-local-time-to-utc-gmt-time.html
You can get time bias from Win32_TimeZone WMI class.
myDate = "9/4/2013 17:23:08"
For Each objItem In GetObject(_
"winmgmts:\\.\root\cimv2").ExecQuery(_
"Select * from Win32_TimeZone")
bias = objItem.Bias
Next
myDate = DateAdd("n", bias, myDate)
WScript.Echo myDate
With SetVarDate the offset change due to transition to daylight saving time (from +060 to +120) occurred one hour too soon. The RegRead(HKLM\..\ActiveTimeBias) method was spot-on. If reproduction is desired, just put the pc clock on a time just before and just after the expected transition time and check the results.
Here is an example that formats the date to UTC as well. Note that you cannot format to a millesecond level with this.
Dim formattedDate
Dim utcDate
Set objShell = WScript.CreateObject("WScript.Shell")
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate(now())
utcDate = dateTime.GetVarDate(false)
wscript.echo "Local Time: " & dateTime
wscript.echo "UTC Time: " & utcDate
formattedDate = DatePart("yyyy",utcDate) & "-" & Right("0" & DatePart("m",utcDate), 2) & "-" & Right("0" & DatePart("d",utcDate), 2)
& "T" & Right("0" & DatePart("h",utcDate), 2) & ":" & Right("0" & DatePart("n",utcDate), 2)
& ":" & Right("0" & DatePart("s",utcDate), 2) & ".000+0000"
wscript.echo formattedDate
'results in a format that looks like this: 1970-01-01T00:00:00.000+0000
set dateTime=Nothing
set objShell=Nothing
Based on above functions - returns a delta-value to be added to the current time to return UTC.
Or call it with DATE+TIME to return UTC.
Call it once and store in a global variable to offset any date/time to UTC.
Conversely Subtract it from any UTC to get the time in the current time zone.
The additional ROUND towards the bottom is an attempt to compensate for floating point errors in the conversion to the nearest second.
Function Time_add_To_get_UTC(Optional DateTime = 0) ''as double
'' Returns value to add to current time to get to UTC
''Based on above functions : )
''return offset from current time to UTC
''https://stackoverflow.com/questions/15887700/utc-time-assignment-in-vbscript/22842128
Dim SWDT ''As SWbemDateTime
Dim dt ''As Date
Set SWDT = CreateObject("WbemScripting.SWbemDateTime")
dt = Date + Time()
SWDT.SetVarDate (dt)
Time_add_To_get_UTC = CDbl(SWDT.GetVarDate(False)) - CDbl(SWDT.GetVarDate(True))
Time_add_To_get_UTC = CDbl(Round(Time_add_To_get_UTC * 24 * 60 * 60, 0) / 24 / 60 / 60)
Time_add_To_get_UTC = DateTime + Time_add_To_get_UTC
End Function
Is there a function to convert a specified number of seconds into a week/day/hour/minute/second time format in vbscript?
eg: 969234 seconds = 1wk 4days 5hrs 13mins 54secs
Dim myDate
dim noWeeks
dim noDays
dim tempWeeks
dim pos
myDate = DateAdd("s",969234,CDate(0))
tempWeeks = FormatNumber(myDate / 7,10)
pos = instr(tempWeeks, ".")
if pos > 1 then
tempWeeks = left(myDate, pos -1)
end if
noWeeks = Cint(tempWeeks)
noDays = Cint(((myDate / 7) - noWeeks) * 7)
wscript.echo noWeeks & "wk " & noDays & "days " & datepart("h", myDate) & "hrs " & datepart("n", myDate) & "mins " & datepart("s", myDate) & "secs"
No built in function to do that.
Here is a quick and dirty one:-
Function SecondsToString(totalSeconds)
Dim work : work = totalSeconds
Dim seconds
Dim minutes
Dim hours
Dim days
Dim weeks
seconds = work Mod 60
work = work \ 60
minutes = work Mod 60
work = work \ 60
hours = work Mod 24
work = work \ 24
days = work Mod 7
work = work \ 7
weeks = work
Dim s: s = ""
Dim renderStarted: renderStarted = False
If (weeks <> 0) Then
renderStarted = True
s = s & CStr(weeks)
If (weeks = 1) Then
s = s & "wk "
Else
s = s & "wks "
End If
End If
If (days <> 0 OR renderStarted) Then
renderStarted = True
s = s & CStr(days)
If (days = 1) Then
s = s & "day "
Else
s = s & "days "
End If
End If
If (hours <> 0 OR renderStarted) Then
renderStarted = True
s = s & CStr(hours)
If (hours = 1) Then
s = s & "hr "
Else
s = s & "hrs "
End If
End If
If (minutes <> 0 OR renderStarted) Then
renderStarted = True
s = s & CStr(minutes)
If (minutes = 1) Then
s = s & "min "
Else
s = s & "mins "
End If
End If
s = s & CStr(seconds)
If (seconds = 1) Then
s = s & "sec "
Else
s = s & "secs "
End If
SecondsToString = s
End Function
You wantto use timer pseudo-variable :
start = timer
Rem do something long
duration_in_seconds = timer - start
wscript.echo "Duration " & duration_in_seconds & " seconds."