Sign in to follow this  
Followers 0
weszzer

Change Date/Time Format

16 posts in this topic

#1 ·  Posted (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 by weszzer

Share this post


Link to post
Share on other sites



Hi,

Why not just try this:

ConsoleWrite(@YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR-1)

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by SmOke_N
added more for bonsantiago
2 people like this

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.

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by weszzer

Share this post


Link to post
Share on other sites

 

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!

Share this post


Link to post
Share on other sites

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

2 people like this

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.

Share this post


Link to post
Share on other sites

A little simpler

StringRegExpReplace($gsDayAdd, "(\S+)\h*(\d\d).*", "$1/$2")

Anyway regex is not necessary in this case :P

1 person likes this

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 :P

Edited 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.

Share this post


Link to post
Share on other sites

I obviously agree, no doubt  :D

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...  :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

The " ; minus one day" in the above posts were bugging me. :rolleyes:

#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

Share this post


Link to post
Share on other sites

Guys, many thanks for the helps I really appreciate your effort..

Cheers

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