weszzer Posted February 4, 2015 Share Posted February 4, 2015 (edited) Hi there, My code below is to get the date today and the time (minus 1 hour), it's give me like; 2015/02/04 15:38:43. How to format this to 2015/02/04/15 or yyyy/mm/dd/hh only (removing the minutes and seconds) "hh" in 24hours format. #Include <Date.au3> Global $gsDayAdd = _DateAdd("h", -1, _NowCalc()) ; minus one day Global $gsDaySave= StringReplace(_DateAdd("h", -1, _NowCalc()),"/","",0,1) ;remove "/" ConsoleWrite($gsDayAdd) Thank you and regards.. Edited February 4, 2015 by weszzer Link to comment Share on other sites More sharing options...
Bone09 Posted February 4, 2015 Share Posted February 4, 2015 Hi, Why not just try this: ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR-1) Link to comment Share on other sites More sharing options...
mikell Posted February 4, 2015 Share Posted February 4, 2015 ? #Include <Date.au3> Global $gsDayAdd = _DateAdd("h", -1, _NowCalc()) ; minus one day $s = StringSplit($gsDayAdd, " ") $d = $s[1] & "/" & StringLeft($s[2], 2) ConsoleWrite($d) weszzer 1 Link to comment Share on other sites More sharing options...
weszzer Posted February 4, 2015 Author Share Posted February 4, 2015 Hi, Why not just try this: ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR-1) that's give me: 2015-02-04--1 , "-1" if the time is the current time is between 12:00 ~ 12:59 AM it should be like 2015-02-04-00 Cheers Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 4, 2015 Moderators Share Posted February 4, 2015 (edited) Hi, Why not just try this: ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR-1) Because if @Hour = 00 then your @hour now = -1, and if the hour is 00 and -1, you also need to -1 MDay, and if MDay is 01 you need to -1 Month and if Month is 01 you need to minus year... fun huh? @weszzer #Include <Date.au3> ;~ 2015/02/04 15:38:43. ;~ How to format this to 2015/02/04/15 Global $gsDayAdd = _DateAdd("h", -1, _NowCalc()) ; minus one day Global $gsFormat = StringRegExpReplace($gsDayAdd, "(.{10})(.)(..)(.+$)", "$1/$3") ConsoleWrite($gsFormat & @CRLF) Edited February 4, 2015 by SmOke_N added more for bonsantiago Bone09 and weszzer 2 Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
weszzer Posted February 4, 2015 Author Share Posted February 4, 2015 ? #Include <Date.au3> Global $gsDayAdd = _DateAdd("h", -1, _NowCalc()) ; minus one day $s = StringSplit($gsDayAdd, " ") $d = $s[1] & "/" & StringLeft($s[2], 2) ConsoleWrite($d) Working great mikell!!, thank you.. and thanks bonsantiago... Link to comment Share on other sites More sharing options...
weszzer Posted February 4, 2015 Author Share Posted February 4, 2015 (edited) Because if @Hour = 00 then your @hour now = -1, and if the hour is 00 and -1, you also need to -1 MDay, and if MDay is 01 you need to -1 Month and if Month is 01 you need to minus year... fun huh? @weszzer #Include <Date.au3> ;~ 2015/02/04 15:38:43. ;~ How to format this to 2015/02/04/15 Global $gsDayAdd = _DateAdd("h", -1, _NowCalc()) ; minus one day Global $gsFormat = StringRegExpReplace($gsDayAdd, "(.{10})(.)(..)(.+$)", "$1/$3") ConsoleWrite($gsFormat & @CRLF) Thanks SmOke_N.. It's working as per your code. Can you explain this to me please.. "(.{10})(.)(..)(.+$)", "$1/$3") " in your code..? Edited February 4, 2015 by weszzer Link to comment Share on other sites More sharing options...
Bone09 Posted February 4, 2015 Share Posted February 4, 2015 Because if @Hour = 00 then your @hour now = -1, and if the hour is 00 and -1, you also need to -1 MDay, and if MDay is 01 you need to -1 Month and if Month is 01 you need to minus year... fun huh? @weszzer #Include <Date.au3> ;~ 2015/02/04 15:38:43. ;~ How to format this to 2015/02/04/15 Global $gsDayAdd = _DateAdd("h", -1, _NowCalc()) ; minus one day Global $gsFormat = StringRegExpReplace($gsDayAdd, "(.{10})(.)(..)(.+$)", "$1/$3") ConsoleWrite($gsFormat & @CRLF) My bad. Well, at least I also learned something. Thanks! Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 4, 2015 Moderators Share Posted February 4, 2015 Your welcome bone09/santiago ... so confused right now. @wes* "(.{10})(.)(..)(.+$)", "$1/$3") " All items in parenthesis are because we want to capture them, to later include or exclude. (.{10}) = grab the first 10 characters of the string (.) = grab the next character (it's a space, I could have done s or h or others, but a dot is grab whatever is there) (..) = grab the next two characters, I could have done .{2} but .. is less work (.+$) = grab the rest of the characters until the end of the string with a greedy "+" quantifier "$1" = include the first captured group (.{10}) in my return string "/" = add a forward slash after my first capture group "$3" = include the 3rd capture group (..) in my return string We exclude $2 (.) and $4 (.+$) because they are not what we want included in our string SorryButImaNewbie and weszzer 2 Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
mikell Posted February 4, 2015 Share Posted February 4, 2015 A little simpler StringRegExpReplace($gsDayAdd, "(\S+)\h*(\d\d).*", "$1/$2") Anyway regex is not necessary in this case weszzer 1 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 4, 2015 Moderators Share Posted February 4, 2015 (edited) Nice mikell as usual... RegEx is NEVER necessary in any case... could do a bunch of StringLeft($gsDayAdd, 10) & blah blah blah, but it's much cleaner at least with the RegEx Edited February 4, 2015 by SmOke_N cleared up my ebonics Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
mikell Posted February 4, 2015 Share Posted February 4, 2015 I obviously agree, no doubt But here it's one StringSplit and one StringLeft - really not a bunch - and certainly easier for the OP to understand And please let me add that I was recently a bit troubled by Melba who said "a good part of understanding Regexes is to know when not to use them." So, well... Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 4, 2015 Moderators Share Posted February 4, 2015 I agree with Melba's statement exactly, in fact, I'm the one that told GeoSoft that. But... I whole heatedly believe that this isn't one of those times. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
mikell Posted February 4, 2015 Share Posted February 4, 2015 Matter of circumstance... This thread is particularly representative : '?do=embed' frameborder='0' data-embedContent>> Link to comment Share on other sites More sharing options...
Malkey Posted February 4, 2015 Share Posted February 4, 2015 The " ; minus one day" in the above posts were bugging me. #include <Date.au3> ; 2015/02/04 15:38:43. ; To this format yyyy/MM/dd/hh Global $sHrSub = _DateAdd("h", -1, "2015/02/04 15:38:43") ; _NowCalc()) ; minus one hour Global $sFormat = StringRegExpReplace($sHrSub, "\h(\d+).+", "/$1") ; "\h" match the only horizontal space present; then, ; "(\d+)" match the following digits only (not ":"), and group capture those digits for later back referencing; then, ; ".+" match all following characters to the end of the line, and in this case, to the end of the string. ; "/$1" Replace all those above matched characters with "/" and the first back reference, which is "$1" (14) from (15 - 1)hr. ; "\1" or "${1}" could also be used to signify the first back reference. ; Note: The pre-space characters in the test string are not matched (are not touched), and remain the same ("2015/02/04"). ConsoleWrite($sFormat & @CRLF) ; Returns 2015/02/04/14 Link to comment Share on other sites More sharing options...
weszzer Posted February 5, 2015 Author Share Posted February 5, 2015 Guys, many thanks for the helps I really appreciate your effort.. Cheers 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