Jump to content

How to add random seconds to Date/Time?


jmp
 Share

Go to solution Solved by mikell,

Recommended Posts

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?

Link to comment
Share on other sites

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)

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Solution

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)

 

Link to comment
Share on other sites

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:

#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

Link to comment
Share on other sites

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 by Musashi
typo

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

1 hour ago, jmp said:

AM/PM Not working in this

Hmm, maybe some locale settings - for me it works, see :

 

DateTime-PM.jpg

DateTime-AM.jpg

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...