jmp Posted December 25, 2023 Posted December 25, 2023 I'm working on extracting date and time data from an Internet Explorer webpage. The format of the date and time on the webpage is "DD/MM/YYYY [ HH:MM:SS]." My objective is to add a random number of seconds to the obtained date and time. Here's the code snippet I've attempted: $iwebdate = $oTds(1).InnerText $sNewDate = _DateAdd('s', Random(2, 8, 1), $iwebdate) MsgBox(0, "Your modified date is", $sNewDate) However, when running this code, it returns 0 for $sNewDate. $iwebdate represents the extracted date and time from the webpage. How can I properly modify this code to add a random number of seconds to the retrieved date and time?
ioa747 Posted December 25, 2023 Posted December 25, 2023 the below works $iwebdate = "2023/01/01 00:00:00" $sNewDate = _DateAdd('s', Random(2, 8, 1), $iwebdate) MsgBox(0, "Your modified date is", $sNewDate) I know that I know nothing
jmp Posted December 25, 2023 Author Posted December 25, 2023 @ioa747 But in my case the format of the date and time on the webpage is "DD/MM/YYYY [ HH:MM:SS]
Musashi Posted December 25, 2023 Posted December 25, 2023 19 minutes ago, jmp said: But in my case the format of the date and time on the webpage is "DD/MM/YYYY [ HH:MM:SS] Try : #include <Date.au3> Local $sDateWebsite, $sDateConverted, $sDateNew $sDateWebsite = "25/12/2023 12:00:30" ; Notation DD/MM/YYYY HH:MM:SS ; convert to Format YYYY/MM/DD HH:MM:SS (needed by _DateAdd) $sDateConverted = StringMid($sDateWebsite,7,4) & "/" & _ StringMid($sDateWebsite,4,2) & "/" & _ StringMid($sDateWebsite,1,2) & " " & _ StringMid($sDateWebsite,12,8) $sDateNew = _DateAdd('s', Random(2, 8, 1), $sDateConverted) MsgBox(0, "Your modified date is : ", $sDateNew) "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
orbs Posted December 25, 2023 Posted December 25, 2023 @jmp. 1) you do not need to use _DateAdd() and thus the date format has no importance in this case. this is because you simply want to add a string to another string. 2) if you are concerned about the data omitting the time part (i.e. the HH:MM:SS part is missing), that is also a simple string check. 3) there are no error check methods in your snippet. you are not checking if $iwebdate (which you designate as integer, why?) actually contains valid data. 4) one cannot help but wonder about the reasoning for this. at least a few eyebrows were raised reading your topic i'm sure. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff
ioa747 Posted December 25, 2023 Posted December 25, 2023 (edited) #include <StringConstants.au3> $iwebdate = "01/01/2023 [ 05:15:00]" $sNewDate = StringLeft($iwebdate, StringInStr($iwebdate, ":", $STR_NOCASESENSEBASIC, -1) - 1) & ":0" & Random(2, 8, 1) & "]" ConsoleWrite("$sNewDate=" & $sNewDate & @CRLF) Edited December 25, 2023 by ioa747 oops I know that I know nothing
Musashi Posted December 25, 2023 Posted December 25, 2023 @Melba23 has provided a nice UDF for Date-Time-Conversions, see : "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
jmp Posted December 25, 2023 Author Posted December 25, 2023 6 minutes ago, ioa747 said: #include <StringConstants.au3> $iwebdate = "01/01/2023 [ 05:15:00]" $sNewDate = StringLeft($iwebdate, StringInStr($iwebdate, ":", $STR_NOCASESENSEBASIC, -1) - 1) & ":0" & Random(2, 8, 1) & "]" ConsoleWrite("$sNewDate=" & $sNewDate & @CRLF) @ioa747 I don't want to change seconds like this, But i want to add 2 to 8 seconds to the seconds, So if second i 15, i want 15+2 to 8.
ioa747 Posted December 25, 2023 Posted December 25, 2023 (edited) #include <StringConstants.au3> $iwebdate = "01/01/2023 [ 05:15:08]" $sNewDate = StringLeft($iwebdate, 19) & StringFormat("%02s", Int(StringTrimRight(StringRight($iwebdate, 3), 1)) + Random(2, 8, 1)) & "]" ConsoleWrite("- $sNewDate = " & $sNewDate & @CRLF) Edited December 25, 2023 by ioa747 Correction I know that I know nothing
Musashi Posted December 25, 2023 Posted December 25, 2023 @jmp : is your notation for the Webdate really DD/MM/YYYY [ HH:MM:SS] or do the brackets stand for 'optional' ? Please post a real result for Webdate. "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
Solution mikell Posted December 25, 2023 Solution Posted December 25, 2023 You might try this - the regex is exactly the one which is in the help file #include <Date.au3> $iwebdate = "24/12/2023 00:00:15" ; convert $c = StringRegExpReplace($iwebdate, '(\d+)/(\d+)/(\d+)(.*)', "$3/$2/$1$4") ; MsgBox(0, "", $c) $sNewDate = _DateAdd('s', Random(2, 8, 1), $c) ; MsgBox(0, "", $sNewDate) ; convert back $c = StringRegExpReplace($sNewDate, '(\d+)/(\d+)/(\d+)(.*)', "$3/$2/$1$4") MsgBox(0, "", $c)
jmp Posted December 25, 2023 Author Posted December 25, 2023 Thank You @mikell How can I format $iwebdate to "Dec 23 2023 05:15AM"?
mikell Posted December 25, 2023 Posted December 25, 2023 For this I highly suggest to use the Melba's UDF , Musachi provided the link some posts above Musashi 1
jmp Posted December 25, 2023 Author Posted December 25, 2023 @mikell I can't find specific example for my need
mikell Posted December 25, 2023 Posted December 25, 2023 Even if the UDF doesn't allow to make your custom conversion in one go, it's certainly doable using several successive steps
jmp Posted December 25, 2023 Author Posted December 25, 2023 4 minutes ago, mikell said: Even if the UDF doesn't allow to make your custom conversion in one go, it's certainly doable using several successive steps @mikellThis code is working for me: expandcollapse popup#include <StringConstants.au3> #include <MsgBoxConstants.au3> #include <Date.au3> Func GetMonthAbbreviation($month) Local $aMonths[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] Return $aMonths[$month] EndFunc $sDateTime = "25/01/2023 18:50:30" ; Split the date and time components $aDateTime = StringSplit($sDateTime, " ") $sDate = $aDateTime[1] ; Extracted date "25/12/2023" $sTime = $aDateTime[2] ; Extracted time "13:00:30" ; Split the date components into day, month, and year $aDate = StringSplit($sDate, "/") $day = $aDate[1] $month = $aDate[2] $year = $aDate[3] ; Split the time components into hour, minute, and second $aTime = StringSplit($sTime, ":") $hour = $aTime[1] $minute = $aTime[2] $second = $aTime[3] ; Get the month abbreviation $sMonthAbbreviation = GetMonthAbbreviation($month) ; Convert 24-hour format to 12-hour format and determine AM/PM $sAmPm = "AM" If $hour > 12 Then $hour -= 12 $sAmPm = "PM" EndIf ; Format the date and time string $sFormattedDateTime = $sMonthAbbreviation & " " & $day & " " & $year & " " & $hour & ":" & $minute & $sAmPm MsgBox(0, "Formatted Date and Time", $sFormattedDateTime) Suggest me if i can short my code
Musashi Posted December 25, 2023 Posted December 25, 2023 (edited) 1 hour ago, jmp said: Suggest me if i can short my code Use @Melba23 ' UDF (as already suggested by me and @mikell): #include <Date.au3> #include "DTC.au3" Local $sIn_Date, $sOut_Date $sIn_Date = "25/12/2023 12:00:30" $sOut_Date = _Date_Time_Convert(_DateAdd('s', Random(2, 8, 1), _Date_Time_Convert($sIn_Date, "dd/MM/yyyy HH:mm:ss", "yyyy/MM/dd HH:mm:ss")), "yyyy/MM/dd HH:mm:ss", "MMM dd yyyy HH:mm:ss TT") MsgBox(0, "Date-Conversion", "Webdate = " & $sIn_Date & @CRLF & "Newdate = " & $sOut_Date & @CRLF) Edited December 25, 2023 by Musashi typo "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
jmp Posted December 25, 2023 Author Posted December 25, 2023 1 hour ago, Musashi said: Use @Melba23 ' UDF (as already suggested by me and @mikell): #include <Date.au3> #include "DTC.au3" Local $sIn_Date, $sOut_Date $sIn_Date = "25/12/2023 12:00:30" $sOut_Date = _Date_Time_Convert(_DateAdd('s', Random(2, 8, 1), _Date_Time_Convert($sIn_Date, "dd/MM/yyyy HH:mm:ss", "yyyy/MM/dd HH:mm:ss")), "yyyy/MM/dd HH:mm:ss", "MMM dd yyyy HH:mm:ss TT") MsgBox(0, "Date-Conversion", "Webdate = " & $sIn_Date & @CRLF & "Newdate = " & $sOut_Date & @CRLF) AM/PM Not working in this
Moderators Melba23 Posted December 25, 2023 Moderators Posted December 25, 2023 jmp, It looks as if it works for me - what exact results so you get which cause you to say there is an error? And have you tried the Beta code that is to be found here? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Musashi Posted December 25, 2023 Posted December 25, 2023 1 hour ago, jmp said: AM/PM Not working in this Hmm, maybe some locale settings - for me it works, see : "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now