Jump to content

How to add random seconds to Date/Time?


Go to solution Solved by mikell,

Recommended Posts

Posted

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?

Posted
  On 12/25/2023 at 8:34 AM, jmp said:

But in my case the format of the date and time on the webpage is "DD/MM/YYYY [ HH:MM:SS]

Expand  

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

Posted

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

  Reveal hidden contents

 

Posted

@Melba23 has provided a nice UDF for Date-Time-Conversions, see

 

 

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

Posted
  On 12/25/2023 at 8:58 AM, 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)

 

Expand  

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

Posted

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

  • Solution
Posted

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)

 

Posted
  On 12/25/2023 at 10:32 AM, 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

Expand  

@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

Posted (edited)
  On 12/25/2023 at 10:37 AM, jmp said:

Suggest me if i can short my code

Expand  

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

Posted
  On 12/25/2023 at 11:48 AM, 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)

 

Expand  

AM/PM Not working in this

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted
  On 12/25/2023 at 1:19 PM, jmp said:

AM/PM Not working in this

Expand  

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

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
  • Recently Browsing   0 members

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