Getting month and year date format in vbscript - vbscript

Can someone please tell me how can we get data in the month and date format.In the below code it shows me the year with the same date one year ago in the format 6/18/2012 ..but I just need the month/year.
LastMonth = DateAdd("m",-12,Date)
Thanks

Use the specialized functions Month(), Year(), ... to get at the private parts of a (variable of type) Date:
>> dt = DateAdd("m",-12,Date)
>> WScript.Echo TypeName(dt), CStr(dt), Month(dt) & "/" & Year(dt)
>>
Date 6/18/2012 6/2012

Related

VBS Object Required Error manipulating strings

I am trying to create a simple script that prompts the user for their birthday, formatted as 10.02.20, then takes that string and turns it into text, such as October 2nd 2020. I have the following code:
Dim bd, message, title ' define variables
title = "What is your birthday?" ' set variable
message = "Format like so: 12.15.07; 3.03.05" ' set variable
bd = InputBox(message, title) ' prompt user
Dim year
year = bd.Substring(6, 2)
Dim month
month = bd.Substring(0, 2)
Dim day
day = bd.Substring(3, 2)
msgbox bd ' for testing
call msgbox(year + month + day) 'also testing
And I am getting an error after the prompt, ...\Desktop\test.vbs(8, 1) Microsoft VBScript runtime error: Object required: '12.23.03' and I am not sure what it means, Object Required.
Any fixes or suggestions would be very much appreciated.
You cannot use Substring in VBScript. You can use Mid instead:
Dim year
year = Mid(bd, 7, 2)
The first character in a string is at position 1, not 0, so I adjusted your parameter from 6 to 7.
Also, to concatenate strings, although + works, you can also use &:
call msgbox(year & month & day)
You can use this function in vbscript : FormatDateTime(date,format)
Parameter Description
date Required.
Any valid date expression (like Date() or Now())
format Optional.
A value that specifies the date/time format to use can take the following values:
0 = vbGeneralDate - Default. Returns date: mm/dd/yyyy and time if specified: hh:mm:ss PM/AM.
1 = vbLongDate - Returns date: weekday, monthname, year
2 = vbShortDate - Returns date: mm/dd/yyyy
3 = vbLongTime - Returns time: hh:mm:ss PM/AM
4 = vbShortTime - Return time: hh:mm
Dim Title,Input
Title = "format date"
Input = InputBox("Enter your date of Birthday !",Title,"10.02.20")
Input = FormatDateTime(Replace(Input,".","/"),1)
MsgBox Input

How can I reformat Date Time output into ISO 8601 [duplicate]

This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 3 years ago.
Using the code below it outputs the following value: 04/10/2019 which is due to the server being set to UK date time settings. However google is reading this date as 10 April 19. So I need to convert it to ISO8601 formate, ie 10/04/2019
Dim FutureDate, TodaysDate
FutureDate = FormatDateTime(Now(),1)
TodaysDate = DateAdd("m", 0, FutureDate)
FutureDate = DateAdd("d", +100, TodaysDate)
"priceValidUntil": "<%= FutureDate %>",
I don't remember ASP supporting this, you can change the format between long date, short date, ... but not to ISO8601
I used something like this to do it (string split and concat):
d = split(FutureDate,"/")
future_iso = d(1) & "/" & d(0) & "/" & d(2)

DateTime Ruby How to format correctly

My project is supposed to fetch specific values from multiple hashes a put those values in a text file. Ideally what I need my code to do is to have every date for the employees be seven days apart, so the text file would look something like this:
"Rachel Thorndike
2017-10-09-T04:29:46-05:00
Stacie Smith
2017-10-16-T04:29:46-05:00"
What this is supposed to do is fetch employee's names and put the time of their "handoff" on the line under them. I looked online and found the DateTime that Ruby features but it looks like whatever I do isn't working. My code is this:
require 'date'
jsonUser["users"].each do |user|
somefile.puts user["user"]["summary"]
print 'Handoff Date + Time: '
parsed = DateTime.strptime(jsonUser["start"], '%d-%m-%Y %H:%M')
utc = parsed.next_day(7).strftime('%d-%m-%Y %H:%M')
puts utc
end
But terminal returns this code with an error 'strptime': invalid date (ArgumentError). Would anybody help me get this code to work the way I want to? Anything that points me to the right direction? With explanations, if it isn't too much.
Thank you so much!
Update
I was able to get the iso8601 to appear under their name. My new code is
require 'date'
jsonUser["users"].each do |user|
somefile.puts user["user"]["summary"]
print 'Handoff Date + Time: '
parsed = DateTime.iso8601(jsonUser["start"])
utc = parsed.next_day(7).iso8601
somefile.puts utc
end
BUT the .next_day method isn't increasing by 7 days that I want too. Thought? I have only got one value that is appearing and that is going under every line. Its the value of jsonUser["start"] + 7 days...so `2017-
10-16T04:29:46-05:00`
This is what parse.next_day(7) gives me.
"Sr Chid
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Ash A
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Ven D
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Abhi S
Handoff Date + Time: 2017-10-16T04:29:46-05:00"
The value of jsonUser["start"] is 2017-10-09T04:29:46-05:00 so the good thing is that it did increase by 7 but it only did it once.
Update for Amadan
require 'date'
date = DateTime.iso8601(jsonUser["start"])
jsonUser["users"].each do |user|
if user["user"]["self"] == nil
nil
else
somefile.puts user["user"]["summary"].gsub(/\w+/, &:capitalize).gsub(/[.]/, ' ')
somefile.print 'Handoff Date + Time: '
date = date.next_day(7)
somefile.puts date.iso8061
end
end

MS Access. Grouping records by date

Using MS Access. I'm trying to write a report, grouped by date into weeks (Mon thru Sunday) and give a total of all transactions during the week.
There are only two tables.
Here is the sql:
SELECT tblBuyer.BuyerTradeDate, [booksold]-[bookpaid]-[booktradefee] AS Outcome
FROM tblSale INNER JOIN tblBuyer ON tblSale.BuyerID = tblBuyer.buyerID;
My query returns two columns, BuyerTradeDate and Outcome.
The image below shows some test data.
Sample output from query
I need the report to display:
Report Output
I'd be grateful for any suggestions. Thanks.
Here is what I've come up with that provides the solution I need.
In the report I've added a BuyerTradeDate header with a txt box containing:
="Week Number: " & Format$([BuyerTradeDate],"ww",0,0)
I've added a BuyerTradeDate Footer with a txt box containing:
="Summary for week beginning: " & " " & [BuyerTradeDate] & " (" & Count(*) & " " & IIf(Count(*)=1,"Trade","Trades") & ")"
The report looks like this:
Report Output
Create a field in your source query or in your report having this expression as control source:
=Weekday([BuyerTradeDate],2)
Then group on this in your report.
To have the first and the last date of the week, use these functions:
Public Function DateWeekFirst( _
ByVal datDate As Date, _
Optional ByVal lngFirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) _
As Date
' Returns the first date of the week of datDate.
' lngFirstDayOfWeek defines the first weekday of the week.
' 2000-09-07. Cactus Data ApS.
' 2003-05-01. System settings used as default.
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek.
DateWeekFirst = DateAdd("d", vbSunday - Weekday(datDate, lngFirstDayOfWeek), datDate)
End Function
Public Function DateWeekLast( _
ByVal datDate As Date, _
Optional ByVal lngFirstDayOfWeek As Long = vbUseSystemDayOfWeek) _
As Date
' Returns the last date of the week of datDate.
' lngFirstDayOfWeek defines the first weekday of the week.
' 2000-09-07. Cactus Data ApS.
' 2003-05-01. System settings used as default.
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek.
DateWeekLast = DateAdd("d", vbSaturday - Weekday(datDate, lngFirstDayOfWeek), datDate)
End Function

Having the day value of date start with 0

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.

Resources