Jump to content

_DateAdd 12 hour time not 24?


Recommended Posts

Seems trivial at first glance, but there is some disagreement on 12noon and 12midnight (see code for the convention I'm using). This just confirms why I prefer 24hr time in my scripts.

#include <Date.au3>

$i = 4    ; hrs to add
;$sDate = "2009/10/03 20:03:03"
;ConsoleWrite($sDate & " plus " & $i & "hrs = " & DateAdd_12hr($i, $sDate) & @LF & "@error=" & @error & @LF)
ConsoleWrite("Current time plus " & $i & "hrs = " & DateAdd_12hr($i) & @LF & "@error=" & @error & @LF)

; based on 12noon as 12pm and 12 midnight as 12am
Func DateAdd_12hr($iAdd, $sDate = "")
    If $sDate = "" Then $sDate = _NowCalc()
    Local $sNewDate = _DateAdd('h', $iAdd, $sDate)
    Local $aDt = StringRegExp($sNewDate, '(\d{4}/\d{2}/\d{2}) (\d{2}):(\d{2}):(\d{2})', 3)
    If Not IsArray($aDt) Then Return SetError(1, 0, '')
    $sNewDate = $aDt[0]
    Local $sMinSec = $aDt[2] & ':' & $aDt[3]
    Local $iHr = Number($aDt[1])
    Switch $iHr
        Case 0
            $sNewDate &= ' 12:' & $sMinSec & ' a.m.'
        Case 12
            $sNewDate &= ' 12:' & $sMinSec & ' p.m.'
        Case 13 To 23
            $sNewDate &= StringFormat(' %02s', $iHr - 12) & ':' & $sMinSec & ' p.m.'
        Case Else
            $sNewDate &= ' ' & $aDt[1] & ':' & $sMinSec & ' a.m.'
    EndSwitch
    Return $sNewDate
EndFunc   ;==>DateAdd_12hr

Edit: 'DateAdd_12hr(4, $sDate)' should be 'DateAdd_12hr($i, $sDate)'

Edited by picaxe
Link to post
Share on other sites
  • Developers

It is not clear for me what you are trying here.

Show some base code you have and where its going wrong so we can adapt it.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

oh opps lol now it works now thanks heaps :)

Anyway to change it to make it show the time 1st then the date

Array $aDt[0] contains the year "yyyy/mm/dd" and elements [1]-[3] contain "hh", "mm" and "ss" you should be able to easily rearrange them the way you want. In response to your message, 24 hour format is unambiguous and can be directly used in calculations. Edited by picaxe
Link to post
Share on other sites
  • 2 weeks later...

this works fine

#include <Date.au3>

$i = 4    ; hrs to add
;$sDate = "2009/10/03 20:03:03"
;ConsoleWrite($sDate & " plus " & $i & "hrs = " & DateAdd_12hr($i, $sDate) & @LF & "@error=" & @error & @LF)
ConsoleWrite("Current time plus " & $i & "hrs = " & DateAdd_12hr($i) & @LF & "@error=" & @error & @LF)

; based on 12noon as 12pm and 12 midnight as 12am
Func DateAdd_12hr($iAdd, $sDate = "")
    If $sDate = "" Then $sDate = _NowCalc()
    Local $sNewDate = _DateAdd('h', $iAdd, $sDate)
    Local $aDt = StringRegExp($sNewDate, '(\d{4}/\d{2}/\d{2}) (\d{2}):(\d{2}):(\d{2})', 3)
    If Not IsArray($aDt) Then Return SetError(1, 0, '')
    $sNewDate = $aDt[0]
    Local $sMinSec = $aDt[2] & ':' & $aDt[3]
    Local $iHr = Number($aDt[1])
    Switch $iHr
        Case 0
            $sNewDate &= ' 12:' & $sMinSec & ' a.m.'
        Case 12
            $sNewDate &= ' 12:' & $sMinSec & ' p.m.'
        Case 13 To 23
            $sNewDate &= StringFormat(' %02s', $iHr - 12) & ':' & $sMinSec & ' p.m.'
        Case Else
            $sNewDate &= ' ' & $aDt[1] & ':' & $sMinSec & ' a.m.'
    EndSwitch
    Return $sNewDate
EndFunc   ;==>DateAdd_12hr

but i would like to change it so it shows the time 1st or at least change it so the date shows as DD/MM/YYYY

Link to post
Share on other sites

can u explane to me how u did this and what all the (.{1,2}}/(.{1.2}} stuff dose?

This should be read with continual reference to the help file - StringRegExpReplace and the StringRegExp sections.

An example string to check created from _DateTimeFormat(_DateAdd('h', 4, _NowCalc()), 0) is

19/10/2009 11:53:23 PM

"(.{1,2})/(.{1,2})/(.{4}) (.{1,2}):(.{1,2}):(.{1,2}) (.{2})" is the regular expression pattern used.

Notice the similarities between the test string and the pattern - two back slashes, a space, two colons, a another space followed by two letters PM. These characters appearing in both test string and pattern act as anchor points to enable correct matching.

The month, day, hours and minutes allow for one or two digits in the pattern with {1,2}, to allow for e.g. 1/1/2008 or 11/12/2009 The year allows for four digits.

The dot,"." is described under thr StringRegExp section. "\d" could have been used in place of the dot in the pattern except for where the "PM" will match.

Each pair of matching brackets, (), are automatically numbered from left to right, starting at one, and are back-referenced in the replace parameter.

" \4:\5:\6 \7 \1/\2/\3" is the text (with back-references) to replace the regular expression matching text with.

Notice in the pattern, the forth pair of brackets, ( ), counting from the left will correspond to the hours in the test string. So in the above example test string "\4" will return 11.

" \4:\5:\6 \7 \1/\2/\3" will return " 11:53:23 PM 19/10/2009"

"\n", "$n", and "${n}", where n is a number, are different way of back-referencing.

Hope that helps.

Link to post
Share on other sites

oh wow thanks 4 that its abit confusing i need to look into it. I have another problem im trying to get it working in a func but its not working 4 me can any1 tell me what im foing wrong its prob something simple.

#include <Date.au3>
$HoursTillStart = DateAdd_12hr(4)
MsgBox(0,"time",$HoursTillStart)
Func DateAdd_12hr($iAdd)
    StringRegExpReplace(_DateTimeFormat(_DateAdd('h', $iAdd, _NowCalc()), 0), _
        "(.{1,2})/(.{1,2})/(.{4}) (.{1,2}):(.{1,2}):(.{1,2}) (.{2})", " \4:\5:\6 \7 \1/\2/\3")
EndFunc
Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...