VBS to format date Date: Tue, 18 Nov 2022 15:57:11 +0000 [duplicate] - date

This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 8 months ago.
So I am trying to format my date properly in a readable format.
Here is my code.
WScript.Echo(DateDiff("s", "01/01/1970 00:00:00", Now()))
Function sprintf(sFmt, aData)
With CreateObject("System.Text.StringBuilder")
.AppendFormat_4 sFmt, (aData)
sprintf = .ToString()
.Length = 0
End With
End Function
Dim CurrTime : CurrTime = Now()
Dim Elapsed : Elapsed = Timer()
Dim MilliSecs : MilliSecs = Right("000" & Int((Elapsed - Int(Elapsed)) * 1000), 3)
'echo "Date: " . date("D, d M Y H:i:s O");
'Date: Tue, 18 Nov 2014 15:57:11 +0000
WScript.Echo "Date: ", sprintf( "{0:yyyy/MM/dd HH:mm:ss}", Array(CurrTime)) & "." & MilliSecs
The output i am trying to achieve is this
Date: Tue, 18 Nov 2022 15:57:11 +0000
But i am missing allot

Function sprintf(sFmt, aData)
With CreateObject("System.Text.StringBuilder")
.AppendFormat_4 sFmt, (aData)
sprintf = .ToString()
.Length = 0
End With
End Function
Dim CurrTime : CurrTime = Now()
WScript.Echo WeekdayName(Weekday(Now()),True) & ", " & Day(Now()) & " " & MonthName(Month(Now()),True) & " " & sprintf( "{0:yyyy HH:mm:ss}", Array(CurrTime)) & " +0000"
Figured out a way to get the output i wanted.

Related

iMacros: How to get the DATE with EVAL

can someone to give me an direction on this, please
I need this with EVAL, because .js not supported by Chrome.
I have the {{!EXTRACT}} and EVAL {{Day1}} {{Day2}} {{Day3}} {{Day4}}.. {{Day10}}..{{Day20}}
I am extracting from the webpage DATE in this format Oct 10, 2022
if {{!EXTRACT}} value is from the past, let's say Oct 1, 2022 then {{Day1}} value will be the next day after local machine current date, in this case Oct 12, 2022 , then {{Day2}} Oct 13, 2022 , {{Day3}} Oct 14, 2022 and continue with how many days I SET to generate
Then
if case {{!EXTRACT}} value is from the future, Oct 23, 2022, then {{Day1}} value will be the next day Oct 24, 2022, then {{Day2}} Oct 25, 2022 , {{Day3}} Oct 26, 2022 and continue with how many days I SET
hope my information is clear
really don't know how to approach this
thank you again
I am using (FCI): iMacros for CR v10.1.1 'PE', CR v105.0.5195.102 (_x64), Win10_x64. ('CR' = 'Chrome' / 'PE' = 'Personal Edition')
Just trying to give value to the SO comunity as so many times someone with experience helped me.
I found the solution
SET !EXTRACT "Oct 21, 2022"
SET Day1 EVAL("var add = 1; var result = new Date(); var resultText = ''; var extract = new Date('{{!EXTRACT}}').setHours(0,0,0,0); var today = new Date().setHours(0,0,0,0); if(today > extract){ result.setDate(new Date(today).getDate() + add); } else {result.setDate(new Date(extract).getDate() + add);} resultText = result.toLocaleString('en-US', {month: 'short'}) + ' ' + result.getDate() + ', ' + result.getFullYear(); resultText;")
SET Day2 EVAL("var add = 2; var result = new Date(); var resultText = ''; var extract = new Date('{{!EXTRACT}}').setHours(0,0,0,0); var today = new Date().setHours(0,0,0,0); if(today > extract){ result.setDate(new Date(today).getDate() + add); } else {result.setDate(new Date(extract).getDate() + add);} resultText = result.toLocaleString('en-US', {month: 'short'}) + ' ' + result.getDate() + ', ' + result.getFullYear(); resultText;")
PROMPT {{Day1}}_{{Day2}}_

Trying to find out how to convert local time into universal time (java), all the answers I found don't seem to work for me

I have this (the code), and the answer (the hour) should be 11.00
The problem is that the answers I am getting are completely wrong;
(wed april 10 13:09:04 ), todays date ...
Also, i guess it needs the lat and long as well, how must I parse this ?
String dateString = 26 + "-" + 4 + "-" + 1926 + " " + 5 + ":00:00 " ;
SimpleDateFormat f = new SimpleDateFormat( dateString );
f.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println( (new Date()) );
String dd = f.format(new Date());
System.out.println( " " ) ;
I've got ;
String d = LocalDateTime
.parse(
"26-04-1926 05:00:00",
DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" )
)
.atOffset(
ZoneOffset.UTC
)
.toString();
System.out.println( d ) ;
But the output is '1926-04-26T05:00Z'
while the hour should be 11:00
Where did I went wrong?
Also, can I get the output (the hour) as an int?

Get today -2 (skipping weekend)

How can I get the Today -2 days (the last 2 working days from now)? but skipping the weekend?
Example #1: Today is February 25, I want February 21
Example #2: Today is February 26, I want February 24
PS: Date format is DD/MM/YYYY
I have this, but the result is going forward, should I use datediff or what?:
<%
Dim d
d = DateAdd("m", 1, Now)
d = "01/" & Month(d) & "/" & Year(d)
d = DateAdd("d", -1, d)
If Weekday(d) = 7 Then
d = DateAdd("d", -1, d)
ElseIf Weekday(d) = 1 Then
d = DateAdd("d", -2, d)
End If
Response.Write "Day: " & d
%>
To get your desired result you need to subtract 3 days on Saturdays, 4 days on Sundays and Mondays, and 2 days on all other days. This can be achieved with something like this:
today = Now
num = Weekday(today, vbWednesday)
d = today - (2 + num\5 + num\6)
response.write "Two working days back: " & d
The Weekday function returns a numeric value for each weekday. By basing the week on Wednesday you can calculate the additional number of days you need to subtract from the current date with integer divisions:
num\5 returns 1 for Saturday, Sunday and Monday, and 0 otherwise.
num\6 returns 1 for Sunday and Monday, and 0 otherwise.
Thus the term 2 + num\5 + num\6 becomes 3 for Saturdays, 4 for Sundays and Mondays, and 2 for all other days.
This might be overkill for what you need but here are two routines I use in my scripts to add or subtract workdays while considering weekends and holidays.
Function AddWorkingDays(dtStart, intDays)
' Start/Default case...
AddWorkingDays = CDate(dtStart)
' If positive days, step forward, otherwise step backward...
Dim intStep, intCount
If intDays > 0 Then intStep = 1 Else intStep = -1
Do While intCount <> intDays
AddWorkingDays = AddWorkingDays + intStep
If IsValidDate(AddWorkingDays) Then intCount = intCount + intStep
Loop
End Function
Function IsValidDate(d)
Dim intWeekday, intMonth, intDay
intWeekday = Weekday(d)
intMonth = Month(d)
intDay = Day(d)
' Weekend dates are not acceptable...
If intWeekday = vbSaturday Or intWeekday = vbSunday Then Exit Function
' Holidays are also not acceptable...
If intMonth = 01 Then If intDay = 01 Then Exit Function ' New Year's Day
If intMonth = 07 Then If intDay = 04 Then Exit Function ' Independence Day
If intMonth = 12 Then If intDay = 25 Then Exit Function ' Christmas Day
' Memorial Day is the last Monday in May...
If intWeekday = vbMonday Then If intMonth = 05 Then If intDay >= 25 Then Exit Function
' ... (Thanksgiving, others) ...
' All tests passed. Date is a valid workday...
IsValidDate = True
End Function

Formatting PowerShell Get-Date inside string

I can't get my head around how formatting a datetime variable inside a string works in PowerShell.
$startTime = Get-Date
Write-Host "The script was started $startTime"
# ...Do stuff...
$endTime = Get-Date
Write-Host "Done at $endTime. Time for the full run was: $( New-TimeSpan $startTime $endTime)."
gives me the US date format while I want ISO 8601.
I could use
$(Get-Date -Format u)
but I want to use $endTime to make the calculation of the timespan correct.
I have tried all permutations of $, (, ), endTime, -format, u, .ToString(...) and .ToShortDate(), but the one that works.
"This is my string with date in specified format $($theDate.ToString('u'))"
or
"This is my string with date in specified format $(Get-Date -format 'u')"
The sub-expression ($(...)) can include arbitrary expressions calls.
Microsoft Documents both standard and custom DateTime format strings.
You can use the -f operator
$a = "{0:D}" -f (get-date)
$a = "{0:dddd}" -f (get-date)
Spécificator Type Example (with [datetime]::now)
d Short date 26/09/2002
D Long date jeudi 26 septembre 2002
t Short Hour 16:49
T Long Hour 16:49:31
f Date and hour jeudi 26 septembre 2002 16:50
F Long Date and hour jeudi 26 septembre 2002 16:50:51
g Default Date 26/09/2002 16:52
G Long default Date and hour 26/09/2009 16:52:12
M Month Symbol 26 septembre
r Date string RFC1123 Sat, 26 Sep 2009 16:54:50 GMT
s Sortable string date 2009-09-26T16:55:58
u Sortable string date universal local hour 2009-09-26 16:56:49Z
U Sortable string date universal GMT hour samedi 26 septembre 2009 14:57:22 (oups)
Y Year symbol septembre 2002
Spécificator Type Example Output Example
dd Jour {0:dd} 10
ddd Name of the day {0:ddd} Jeu.
dddd Complet name of the day {0:dddd} Jeudi
f, ff, … Fractions of seconds {0:fff} 932
gg, … position {0:gg} ap. J.-C.
hh Hour two digits {0:hh} 10
HH Hour two digits (24 hours) {0:HH} 22
mm Minuts 00-59 {0:mm} 38
MM Month 01-12 {0:MM} 12
MMM Month shortcut {0:MMM} Sep.
MMMM complet name of the month {0:MMMM} Septembre
ss Seconds 00-59 {0:ss} 46
tt AM or PM {0:tt} ““
yy Years, 2 digits {0:yy} 02
yyyy Years {0:yyyy} 2002
zz Time zone, 2 digits {0:zz} +02
zzz Complete Time zone {0:zzz} +02:00
: Separator {0:hh:mm:ss} 10:43:20
/ Separator {0:dd/MM/yyyy} 10/12/2002
Instead of using string interpolation you could simply format the DateTime using the ToString("u") method and concatenate that with the rest of the string:
$startTime = Get-Date
Write-Host "The script was started " + $startTime.ToString("u")

VBScript How can I Format Date?

I want the date to look like MM-DD-YYYY instead of MM/DD/YYYY.
0 = vbGeneralDate - Default. Returns date: mm/dd/yy and time if specified: hh:mm:ss PM/AM.
1 = vbLongDate - Returns date: weekday, monthname, year
2 = vbShortDate - Returns date: mm/dd/yy
3 = vbLongTime - Returns time: hh:mm:ss PM/AM
4 = vbShortTime - Return time: hh:mm
d=CDate("2010-02-16 13:45")
document.write(FormatDateTime(d) & "<br />")
document.write(FormatDateTime(d,1) & "<br />")
document.write(FormatDateTime(d,2) & "<br />")
document.write(FormatDateTime(d,3) & "<br />")
document.write(FormatDateTime(d,4) & "<br />")
If you want to use another format you will have to create your own function and parse Month, Year, Day, etc and put them together in your preferred format.
Function myDateFormat(myDate)
d = TwoDigits(Day(myDate))
m = TwoDigits(Month(myDate))
y = Year(myDate)
myDateFormat= m & "-" & d & "-" & y
End Function
Function TwoDigits(num)
If(Len(num)=1) Then
TwoDigits="0"&num
Else
TwoDigits=num
End If
End Function
edit: added function to format day and month as 0n if value is less than 10.
Suggest calling 'Now' only once in the function to guard against the minute, or even the day, changing during the execution of the function.
Thus:
Function timeStamp()
Dim t
t = Now
timeStamp = Year(t) & "-" & _
Right("0" & Month(t),2) & "-" & _
Right("0" & Day(t),2) & "_" & _
Right("0" & Hour(t),2) & _
Right("0" & Minute(t),2) ' '& _ Right("0" & Second(t),2)
End Function
The output of FormatDateTime depends on configuration in Regional Settings in Control Panel. So in other countries FormatDateTime(d, 2) may for example return yyyy-MM-dd.
If you want your output to be "culture invariant", use myDateFormat() from stian.net's solution. If you just don't like slashes in dates and you don't care about date format in other countries, you can just use
Replace(FormatDateTime(d,2),"/","-")
'for unique file names I use
Dim ts, logfile, thisScript
thisScript = LEFT(Wscript.ScriptName,LEN(Wscript.ScriptName)-4) ' assuming .vbs extension
ts = timeStamp
logfile = thisScript & "_" & ts
' ======
Function timeStamp()
timeStamp = Year(Now) & "-" & _
Right("0" & Month(Now),2) & "-" & _
Right("0" & Day(Now),2) & "_" & _
Right("0" & Hour(Now),2) & _
Right("0" & Minute(Now),2) ' '& _ Right("0" & Second(Now),2)
End Function
' ======
This snippet also solve this question with datePart function. I've also used the right() trick to perform a rpad(x,2,"0").
option explicit
Wscript.Echo "Today is " & myDate(now)
' date formatted as your request
Function myDate(dt)
dim d,m,y, sep
sep = "-"
' right(..) here works as rpad(x,2,"0")
d = right("0" & datePart("d",dt),2)
m = right("0" & datePart("m",dt),2)
y = datePart("yyyy",dt)
myDate= m & sep & d & sep & y
End Function
Although answer is provided I found simpler solution:
Date:
01/20/2017
By doing replace
CurrentDate = replace(date, "/", "-")
It will output:
01-20-2017
For anyone who might still need this in the future. My answer is very similar to qaweb, just a lot less intimidating. There seems to be no cool automatic simple function to formate date in VBS. So you'll have to do it manually. I took the different components of the date and concatenated them together.
Dim timeStamp
timeStamp = Month(Date)&"-"&Day(Date)&"-"&Year(Date)
run = msgbox(timeStamp)
Which will result in 11-22-2019 (depending on the current date)