Jump to content

Change Date/Time Format


weszzer
 Share

Recommended Posts

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
Link to comment
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

Link to comment
Share on other sites

  • Moderators

 

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

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

 

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
Link to comment
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!

Link to comment
Share on other sites

  • Moderators

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

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

  • Moderators

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.

Link to comment
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...  :)

Link to comment
Share on other sites

  • Moderators

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

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...