Sign in to follow this  
Followers 0
PhilipG

Format date string

8 posts in this topic

Hi everyone

I have a string that contains a date. The date is formated "yyyy/MM/dd".

Is there any simple way to change the format to "d MMM -YY"

Ifound something like _GUICtrlDTP_SetFormat() But that just seems to work on actual controls witch this isn't!

Would be thankful over some help!

Share this post


Link to post
Share on other sites



Best way would be a RegEx, but this should also do it....

$sDate = "2009/12/31"
$aDate = StringSplit($sDate,"/")
$sDate_New = $aDate[3] & " " & $aDate[2] & " " & StringRight($aDate[1],2)
MsgBox(0,"",$sDate & @crlf & $sDate_New)

Share this post


Link to post
Share on other sites

#include <Date.au3>

; "yyyy/mm/dd" to "d mmm-yy"format
$sDate = "2009/12/01"
$aDate = StringSplit($sDate, "/")
$sDate_New = Number($aDate[3]) & " " & _DateToMonth($aDate[2], 1) & "-" & StringRight($aDate[1], 2)

MsgBox(0, "Date Format", '"yyyy/mm/dd" = ' & $sDate & @CRLF & '"d mmm-yy" = ' & $sDate_New & @CRLF & "Reg Exp Rep = " & _
        Execute(StringRegExpReplace($sDate, "(.{2})(.{2})/(.{2})/(.{2})", _
        'number("\4") & " " & _DateToMonth("\3", 1) & "-" & "\2"')))

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Try this:

#include <DateTimeConstants.au3> ;For the date control
$dateinput=GUICtrlCreateDate(@YEAR&"/"&@MON&"/"&@MDAY, 670, 67, 155, 20) ;Create the control- for reference
$DTM_SETFORMAT_ = 0x1032 ;Will start to change the style
$style = "d MMM yy"
Edited by Crash

JPGRARMouse Lock | My website | Thanks so much for your help! ❤️

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Try this:

#include <DateTimeConstants.au3> ;For the date control
$dateinput=GUICtrlCreateDate(@YEAR&"/"&@MON&"/"&@MDAY, 670, 67, 155, 20) ;Create the control- for reference
$DTM_SETFORMAT_ = 0x1032 ;Will start to change the style
$style = "d MMM yy"

Here is another example of Crash's method.

Local $Date = "1953/04/05 14:30:11.6"

MsgBox(0, "Date Format", 'Date = ' & $Date & @CRLF & _
    '"d MMM-yy" ' & @tab & " = " & _DateFormat($Date, "d MMM-yy") & @CRLF & _
    '"dddd, d MMMM, yyyy" = ' & _DateFormat($Date, "dddd, d MMMM, yyyy") & @CRLF & _
    '"dd/MM/yyyy" ' & @tab & " = " & _DateFormat($Date, "dd/MM/yyyy") & @CRLF & _
    '"HH-mm-ss or h:mm tt"" = ' & _DateFormat($Date, "HH-mm-ss or h:mm tt"))


Func _DateFormat($Date, $style)
    Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
    Local $idDate = GUICtrlCreateDate($Date, 10, 10, 185, 20)
    GUICtrlSendMsg($idDate, 0x1032, 0, $style)
    Local $sReturn = GUICtrlRead($idDate)
    GUIDelete($hGui)
    Return $sReturn
EndFunc ;==>_DateFormat

Edit: Added time element to MsgBox display for completeness.

Edit2: Added GuiDelete to function as per KaFu's suggestion Post #6.

Edited by Malkey

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Here is another example of Crash's method.

Nice way ;). But better delete the GUI after the function is finished?

Edit: For code see above

Edited by KaFu

Share this post


Link to post
Share on other sites

Thanks alot! The problem is solved ;)

Share this post


Link to post
Share on other sites

Kafo: Wow, even a useless code can become a useful one when it's passed through your hands!

PhilipG: Yohohohoho, I'm glad. Credits go to me!! Jk jk.. Credits still goes to others. I did nothing ;)


JPGRARMouse Lock | My website | Thanks so much for your help! ❤️

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