13lack13lade Posted October 28, 2013 Share Posted October 28, 2013 hi guys so automating an entering of a date. when i use _NowDate() it gives the result - 28/10/2013 however i am only wanting it to 281013 - is there a way to format nowdate to produce this? if not how can i accomplish this? it must be ddmmyy exact format with no slashes. i also need to to display yesterdays date in the same format Link to comment Share on other sites More sharing options...
abberration Posted October 28, 2013 Share Posted October 28, 2013 A quick way of doing this would to use StringReplace and StringSplit. Example: #include <date.au3> $date = StringReplace(_NowDate(), "/", "") MsgBox(0, "Today", $date) $yesterday = StringSplit(_NowDate(), "/", 2) $subtractDay = Number($yesterday[1]) - 1 $yesterdayDate = $yesterday[0] & $subtractDay & $yesterday[2] MsgBox(0, "Yesterday", $yesterdayDate) Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
13lack13lade Posted October 28, 2013 Author Share Posted October 28, 2013 (edited) thank you for that - i am not great at coding but learning string replacements etc atm but still newb lol that worked however is there anyway to change 2013 to just 13? like a trim function perhaps? Edited October 28, 2013 by 13lack13lade Link to comment Share on other sites More sharing options...
czardas Posted October 28, 2013 Share Posted October 28, 2013 (edited) Look at StringTrimLeft() in the help file. Also see StringRight(). Two alternative methods. Edited October 28, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
abberration Posted October 28, 2013 Share Posted October 28, 2013 Here's a more advanced version. Hopefully, it will show you how functions can work: #include <date.au3> ; _GetDate is a function that is written below. We can pass information to a function and use it to return a result. $date = _GetDate("") ; $date is a variable calling the function _GetDate. MsgBox(0, "Today", $date) $date = _GetDate("Yesterday") ; Pass the word "Yesterday" to the function below MsgBox(0, "Yesterday", $date) Func _GetDate($functionPassVariable) $date = _NowDate() ; Today's date $dateSplit = StringSplit($date, "/", 2) ; Split string If $functionPassVariable = "Yesterday" Then ; If we sent the word "Yesterday", then subtract 1 day from the date $dateSplit[1] = Number($dateSplit[1]) - 1 EndIf ; The next line uses StringTrimLeft to shorten 2013 to just 13 Return($dateSplit[0] & $dateSplit[1] & StringTrimLeft($dateSplit[2], 2)) ; Returns the date back to the varible that called this function EndFunc Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
kylomas Posted October 28, 2013 Share Posted October 28, 2013 (edited) 13lack13lade, If you want to do date arithmetic then you should use _nowcalc as it returns a date in the format that _dateadd() and _datediff() expect. The following does what you want...see the comments in the code... #include <date.au3> local $mydate, $offset $mydate = cnvtdate(-2) ConsoleWrite('Two days ago - ' & $mydate & @LF) $mydate = cnvtdate() ConsoleWrite('Today - ' & $mydate & @LF) $mydate = cnvtdate(15) ConsoleWrite('Fifteen days from now - ' & $mydate & @LF) func cnvtdate($offset = 0) $date = _Dateadd('D', $offset, _nowcalcdate()) ; get date with offset $adate = stringsplit($date,'/',3) ; split date into parts for rformatting return $adate[2] & $adate[1] & stringright($adate[0],2) ; return date / stringright returns only the two rightmost bytes of the year endfunc @abberation - what if the date is 10/01/2013? kylomas edit : simplified code pre mikells comments below Edited October 28, 2013 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
mikell Posted October 28, 2013 Share Posted October 28, 2013 (edited) Hum there is a trick because of the time formats so you should use _NowCalcDate() #include <date.au3> $today = _NowCalcDate() MsgBox(0, "today", _Convert($today) ) $yesterday = _DateAdd('d', -1, $today) MsgBox(0, "yesterday", _Convert($yesterday) ) Func _Convert($date) Return StringRegExpReplace($date, '\d\d(\d\d)/(\d\d)/(\d\d)', '$3$2$1') EndFunc Straight from the example in the helpfile for StringRegExpReplace Edited October 28, 2013 by mikell Link to comment Share on other sites More sharing options...
kylomas Posted October 28, 2013 Share Posted October 28, 2013 (edited) Hi mikell, Yes, I stayed away form regexp because i am not great at coding but learning string replacements etc and the thread was already down that path. The point was to show how to present the date in a format suitable for further processing and an example of ome simple string processing. Thanks for your input and I agree, _nowcalcdate() is easier for a beginner to work with! kylomas Edited October 28, 2013 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
mikell Posted October 28, 2013 Share Posted October 28, 2013 Hi kylomas This regex is pretty simple so it seemed a good opportunity to start with But using your code with _NowCalcDate() -- which allows an easier use of _Dateadd() -- is a nice way Link to comment Share on other sites More sharing options...
kylomas Posted October 28, 2013 Share Posted October 28, 2013 As simple as learning Klingon, for some of us anyway!! Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
czardas Posted October 28, 2013 Share Posted October 28, 2013 (edited) Guys this is a little too deep. This is much simpler. ; Global $sDate = "28/10/2013" $sDate = StringReplace($sDate, "/", "") ; Replace "/" with an empty string MsgBox(0, "Removed Delimiter", $sDate) $sDate = StringLeft($sDate, 4) & StringRight($sDate, 2) ; Concatenation MsgBox(0, "Removed century", $sDate) Edited October 28, 2013 by czardas kylomas 1 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
kylomas Posted October 28, 2013 Share Posted October 28, 2013 Doh! K.I.S.S. czardas and 13lack13lade 2 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
mikell Posted October 28, 2013 Share Posted October 28, 2013 czardas, And what about yesterday ? Link to comment Share on other sites More sharing options...
czardas Posted October 28, 2013 Share Posted October 28, 2013 (edited) mikell, your example with regexp is probably the best approach. I just wanted to chime in with the discussion and answer a more fundamental question. ... is there anyway to change 2013 to just 13? like a trim function perhaps? Edited October 28, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
13lack13lade Posted October 28, 2013 Author Share Posted October 28, 2013 Func _Convert($date) Return StringRegExpReplace($date, '\d\d(\d\d)/(\d\d)/(\d\d)', '$3$2$1') EndFunc $today = _NowCalcDate() $yesterday = _DateAdd('d', -1, $today) you guys are legends! thanks for the help. czardas 1 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