I am editing a VBScript so it will take yesterdays date add a string to the front off it and search for that file before moving it to a folder.
sDate = day(date)-1
sName= "Blaa" & "_" & sDate
Using the above bits of code I would get a result - Blaa_10 or Blaa_9
The issue is the files, I want it to search for when under 10 would be named as Blaa_09.
Is there anyway I can format the value day so when it is under 10 it starts with 0? Think I could write an If statement to do this but was hoping there is another way.
The canonical way to left-pad a day of month with a zero in VBScript looks like this:
Right("0" & Day(Date), 2)
Just went with
sDay = day(date) -1
IF sDay < 10 Then
sDate = "0"& sDay & monthname(month(DateAdd("m",-1,Date)), True) & year(date)
WScript.Echo "Date = " & sDate
This worked fine, if anyone has an easier way please feel free to share.
Related
I have the following Expression in the Header of a report and while the report is working fine, the expression returns #ERROR... 'Argument Month is not a valid value.'
= IIF
(
Month(Today) = 1,
"December, " & Year(Today) -1,
MonthName(Month(Today) -1) & ", " & Year(Today)
)
In January, I want the Text Box to display "December" & the Previous Year.
For all other months, I want to display the current Month Name and the current Year.
I would appreciate any help or pointers.
Thanks!!
I probably should have worked harder before posting my question but it was urgent and I wasn't sure I'd be able to find an answer.
This is how I was able to solve the problem:
= IIF
(
Month(Today) = 1,
MonthName(Month(DateAdd("M",-1,Today))) & " " & Today.AddYears(-1).Year,
MonthName(Month(DateAdd("M",-1,Today))) & " " & Today.AddYears(0).Year
)
Thanks!
You were close, I think this should work
=IIF(Month(Today) = 1, "December, " & Year(Today) -1, MonthName(Month(Today)) & ", " & Year(Today))
I am trying to save excel file with previous date in vbscript.
Fill has been save but date format not like 5-Jun-2019.
Please help me..
MyDateFormat = Year(now) & Right("0" & Month(Now), 2) & Right("0" & Day(now)-1, 2)
wbm.SaveAs "C:\report " & MyDateFormat & ".xlsx"
Expected Actual Result like:5-Jun-2019
date-1 gives yesterday "05.06.2019" in my case, this will not let you encounter day = 00, rest should be done by formatting
I have to replicate functionality in C# from an old asp classic codebase.
The codebase seems to assume that the year can have up to 54 weeks and uses the DateAdd method with the "ww" option to figure out the "week ID" for purposes of categorising some files.
All of the online documentation I have found seems to ignore this question and a straightforward reading of it suggests that adding weeks is the same as adding (weeks * 7) days, however, I don't want to break backwards compatibility. I think that the old code has an off by one error but I don't want to assume it. It's possible for a year to have its days in 54 different weeks but I don't think that doing a DateAdd with a value greater than 52 ever makes sense.
My question is, how does the
DateAdd("ww", x, date)
work?
Is it the equivalent of
date.AddDays(7 * x)
in C#?
Are there edge cases where the first of January falls on (for example) a Saturday and DateAdd("ww", 53, date) could return a valid date in the same year?
I can tell you that DateAdd("ww", i, date) is equivalent to DateAdd("d", 7*i, date); therefore, provided that both functions in C# and vbScript have the same meaning, they must be equivalent.
To ensure DateAdd("ww", i, date) is equivalent to DateAdd("d", 7*i, date) I used the script bellow, it cycles 2 years and for each day, it cycles for 104 weeks if adding days or adding weeks mismatch somehow, it raises an error.
date0 = Date()
For i = 0 to 2 * 365
date1 = DateAdd("d", i, date0)
For j = 0 to 2 * 53
date2 = DateAdd("d", j * 7, date1)
date3 = DateAdd("WW", j, date1)
assert = DateDiff("s", date2, date3)
If assert <> 0 Then
errMsg = _
"OPS addDays And addWeeks unmatched" &_
vbCrLf &_
CStr(date1) & vbTab &_
CStr(date2) & vbTab &_
CStr(date3) & vbTab &_
assert & vbTab
Call Err.Raise(vbObjectError + 10, "Test DateAdd(""ww"", i, date)", errMsg)
End If
Next
Next
So I guess you can use date.AddDays(7 * x) to replace DateAdd("ww", x, date) with no concerns in mismatching dates.
I am new to vb express and looking for a way to read two lines in a text file get the difference between then and loop it till the end its a simple clock in clock out system which store each persons clock on and off time in a text file like so
03/11/2014 09:55:02
03/11/2014 14:55:02
03/11/2014 16:55:02
03/11/2014 19:55:02
04/11/2014 09:00:02
04/11/2014 13:00:00
I know I use the DateDiff to get the time but I only want them to work out the difference between line 1 and 2 then 3 and 4 and add them all up is it possible to do that without over complicating things?
I guys I have worked it out I have done this by reading the text filed line by line in a loop at the moment I have not put any validation in to show people who have forgot but the basics are there
Dim FILE_NAME As String = "times\08.txt"
Dim start As DateTime
Dim finish As DateTime
Dim total
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
start = objReader.ReadLine() & vbNewLine
finish = objReader.ReadLine() & vbNewLine
duration = DateDiff(DateInterval.Minute, start, finish)
total = duration + total
Loop
Label2.Text = total
I am trying to get the system time in 24 hour format using VBScript. I had gone through the documents and found nothing. I need to compare the system date and time with my data and need to check the differences.
WScript.Echo right("0" & hour(time),2) & ":" & right("0" & minute(time),2)
WScript.Echo FormatDateTime(time, vbShortTime)
VBScript's Date datatype is format-agnostic. You can calculate the difference between the system time and some other timestamp using the DateDiff function, as long as VBScript recognizes the format of the other timestamp. Example (calculating the difference in seconds):
>>> systime = Now
>>> WScript.Echo systime
26.11.2013 12:48:52
>>> WScript.Echo DateDiff("s", systime, "11/25/2013 23:16:52")
-48720
>>> WScript.Echo DateDiff("s", systime, "25.11.2013 23:16:52")
-48720