epicfail Posted October 2, 2009 Share Posted October 2, 2009 is there away to make it so _DateAdd shows the time in 12 hour not 24? Link to comment Share on other sites More sharing options...
furrycow Posted October 2, 2009 Share Posted October 2, 2009 (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 October 2, 2009 by furrycow Instant Lockerz Invite - www.instantlockerzinvite.co.uk Link to comment Share on other sites More sharing options...
jvanegmond Posted October 2, 2009 Share Posted October 2, 2009 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". github.com/jvanegmond Link to comment Share on other sites More sharing options...
epicfail Posted October 2, 2009 Author Share Posted October 2, 2009 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. Link to comment Share on other sites More sharing options...
epicfail Posted October 3, 2009 Author Share Posted October 3, 2009 i dont see how u can use _DateTimeFormat with _DateAdd Link to comment Share on other sites More sharing options...
picaxe Posted October 3, 2009 Share Posted October 3, 2009 (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 October 3, 2009 by picaxe Link to comment Share on other sites More sharing options...
epicfail Posted October 3, 2009 Author Share Posted October 3, 2009 hey thanks 4 that but it dosnt read the now time of the pc. Link to comment Share on other sites More sharing options...
Developers Jos Posted October 3, 2009 Developers Share Posted October 3, 2009 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 comment Share on other sites More sharing options...
picaxe Posted October 3, 2009 Share Posted October 3, 2009 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) Link to comment Share on other sites More sharing options...
epicfail Posted October 3, 2009 Author Share Posted October 3, 2009 oh opps lol now it works now thanks heaps Anyway to change it to make it show the time 1st then the date Link to comment Share on other sites More sharing options...
picaxe Posted October 3, 2009 Share Posted October 3, 2009 (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 October 3, 2009 by picaxe Link to comment Share on other sites More sharing options...
epicfail Posted October 3, 2009 Author Share Posted October 3, 2009 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 Link to comment Share on other sites More sharing options...
epicfail Posted October 12, 2009 Author Share Posted October 12, 2009 bump Link to comment Share on other sites More sharing options...
Malkey Posted October 12, 2009 Share Posted October 12, 2009 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 ) ; Link to comment Share on other sites More sharing options...
epicfail Posted October 12, 2009 Author Share Posted October 12, 2009 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 comment Share on other sites More sharing options...
Malkey Posted October 12, 2009 Share Posted October 12, 2009 And this works fine. #include <Date.au3> ConsoleWrite(StringRegExpReplace(_DateTimeFormat(_DateAdd('h', 4, _NowCalc()), 0), _ "(.{1,2})/(.{1,2})/(.{4}) (.{1,2}):(.{1,2}):(.{1,2}) (.{2})", " \4:\5:\6 \7 \1/\2/\3") & @CRLF) Link to comment Share on other sites More sharing options...
epicfail Posted October 19, 2009 Author Share Posted October 19, 2009 can u explane to me how u did this and what all the (.{1,2}}/(.{1.2}} stuff dose? Link to comment Share on other sites More sharing options...
Malkey Posted October 19, 2009 Share Posted October 19, 2009 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 comment Share on other sites More sharing options...
epicfail Posted October 20, 2009 Author Share Posted October 20, 2009 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 comment Share on other sites More sharing options...
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