Sign in to follow this  
Followers 0
epicfail

_DateAdd 12 hour time not 24?

19 posts in this topic

is there away to make it so _DateAdd shows the time in 12 hour not 24?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

is there away to make it so _DateAdd shows the time in 12 hour not 24?

Just minus 12 when the hour is greater than 12

Edited by furrycow

Instant Lockerz Invite - www.instantlockerzinvite.co.uk

Share this post


Link to post
Share on other sites

If you are just looking for displaying time in the AM-PM system, there is a function called _DateTimeFormat. It displays the time in the current users time settings.

For example I will see the time as "14:34" and Americans will see "2:34 PM".

Share this post


Link to post
Share on other sites

well i need to be able to use _DateAdd so i can add like 4 hours to the time now and then turn it in to am/pm i will have a look at that thanks.

Share this post


Link to post
Share on other sites

i dont see how u can use _DateTimeFormat with _DateAdd

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

hey thanks 4 that but it dosnt read the now time of the pc.

Share this post


Link to post
Share on other sites

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


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

hey thanks 4 that but it dosnt read the now time of the pc.

The default for param 2 in func DateAdd_12hr is _NowCalc. Uncomment this line in the code to test with _NowCalc

ConsoleWrite("Current time plus " & $i & "hrs = " & DateAdd_12hr($i) & @LF & "@error=" & @error & @LF)

Share this post


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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

i cant seem to rearrange them right i move the time in front but the am dosnt move and after the time adds 4 hours it changes the time to 30:00

Share this post


Link to post
Share on other sites

bump

Share this post


Link to post
Share on other sites

i dont see how u can use _DateTimeFormat with _DateAdd

An example of how to can use _DateTimeFormat with _DateAdd.

;
#include <Date.au3>

$sDateTime = "2008/12/31 12:30:00"

$sNewDate = _DateTimeFormat(_DateAdd( 'h',12, $sDateTime),0)

ConsoleWrite($sNewDate & @CRLF)
MsgBox( 4096, "",  $sDateTime & " +12 hours = " & @CRLF & $sNewDate )
;

Share this post


Link to post
Share on other sites

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

Share this post


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?

Share this post


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.

Share this post


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

Share this post


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
Sign in to follow this  
Followers 0