Sign in to follow this  
Followers 0
13lack13lade

_NowDate Format Help

15 posts in this topic

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

Share this post


Link to post
Share on other sites



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)

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by 13lack13lade

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Look at StringTrimLeft() in the help file. Also see StringRight(). Two alternative methods. ;)

Edited by czardas

Share this post


Link to post
Share on other sites

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

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 by czardas
1 person likes this

Share this post


Link to post
Share on other sites

Doh!   K.I.S.S.

2 people like this

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

Share this post


Link to post
Share on other sites

czardas,

And what about yesterday ? :idiot:

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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. 
1 person likes this

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