ur

Date in particular format

4 posts in this topic

When I tried the below code, I am getting the date in mm-dd-yyyy format.

But I require dd-mm-yyyy format.

Can anyone suggest how to do that.

Func TodaysDate()
    return StringReplace(_DateTimeFormat(_NowCalc(), 2), "/", "-")
EndFunc

Earlier I tried dd-mmm-yyyy format and it is working with below code.

Func TodaysDate()
    $NewDate = _DateTimeFormat(_NowCalcDate(),1)
    $Array = StringSplit( $NewDate , ',' )
    _ArrayDelete($Array, 0)
    _ArrayDelete($Array, 0)
    $Array1 = StringSplit($Array[0],' ')
    RemoveEmptyArrayLines($Array1)
    $Date =  StringStripWS($Array1[2]&"-"&StringLeft($Array1[1], 3)&"-"&$Array[1], $STR_STRIPALL)
    return $Date
EndFunc

Which is unnecessarily complicated I feel., the approach

Now I need format of dd-mm-yyyy.

Can anyone suggest how to do this.

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

Don't know whether it is a good approach or not.

The below code is working.....

This is the code I added to convert dd-mmm-yyyy to dd-mm-yyyy.

;To convert mmm to mm format
Func changeDateformat($sText)
Local $sMsg = StringStripWS($sText, $STR_STRIPALL)

Switch $sMsg
Case "Jan"
$sMsg = 1
Case "Feb"
$sMsg = 2
Case "Mar"
$sMsg = 3
Case "Apr"
$sMsg = 4
Case "May"
$sMsg = 5
Case "Jun"
$sMsg = 6
Case "Jul"
$sMsg = 7
Case "Aug"
$sMsg = 8
Case "Sep"
$sMsg = 9
Case "Oct"
$sMsg = 10
Case "Nov"
$sMsg = 11
Case "Dec"
$sMsg = 12
EndSwitch

return $sMsg
EndFunc

Func TodaysDate()
    $NewDate = _DateTimeFormat(_NowCalcDate(),1)
    $Array = StringSplit( $NewDate , ',' )
    _ArrayDelete($Array, 0)
    _ArrayDelete($Array, 0)
    $Array1 = StringSplit($Array[0],' ')
    RemoveEmptyArrayLines($Array1)
    ;Will return the present day's date with format dd-mmm-yyyy
    ;$Date =  StringStripWS($Array1[2]&"-"&StringLeft($Array1[1], 3)&"-"&$Array[1], $STR_STRIPALL)
    $Date =  StringStripWS($Array1[2]&"-"&changeDateformat(StringLeft($Array1[1], 3))&"-"&$Array[1], $STR_STRIPALL)
    return $Date
EndFunc


Func RemoveEmptyArrayLines(ByRef $arrLines)
  $intCount = 1
  While $intCount < UBound($arrLines)
         $arrLines[$intCount] = StringStripWS($arrLines[$intCount],$STR_STRIPLEADING + $STR_STRIPTRAILING)
         If StringLen($arrLines[$intCount])=0 Then
            _ArrayDelete($arrLines, $intCount)
            $intCount = $intCount - 1
         EndIf
         $intCount = $intCount + 1
   WEnd
EndFunc

 

Edited by ur

Share this post


Link to post
Share on other sites

I forgot add this function to my question.

 

 

Func RemoveEmptyArrayLines(ByRef $arrLines)
  $intCount = 1
  While $intCount < UBound($arrLines)
         $arrLines[$intCount] = StringStripWS($arrLines[$intCount],$STR_STRIPLEADING + $STR_STRIPTRAILING)
         If StringLen($arrLines[$intCount])=0 Then
            _ArrayDelete($arrLines, $intCount)
            $intCount = $intCount - 1
         EndIf
         $intCount = $intCount + 1
   WEnd
EndFunc

 

Share this post


Link to post
Share on other sites

Reply to post #1 - Date to dd-MM-yyyy.

#include <Date.au3>

MsgBox(0, "Result", "Date: " & _TodaysDate() & @CRLF)

Func _TodaysDate()
    Return StringRegExpReplace(_NowCalc(), "(\d{4})/(\d{2})/(\d{2}) (\d{2}):(\d{2}):(\d{2})", "${3}-${2}-${1}")
EndFunc   ;==>_TodaysDate

 

Reply to post #2 - Date to dd-MMM-yyyy.

#include <GUIConstantsEx.au3>
#include <GuiDateTimePicker.au3>
#include <Date.au3>

ConsoleWrite(_DateFormat("2016/12/21", "dd-MMM-yyyy") & @CRLF)
ConsoleWrite("==================" & @CRLF)
ConsoleWrite(_DateFormat("2016/12/21 13:11:02", "ddd MMM dd, yyyy hh:mm:ss tt gg") & @CRLF)
ConsoleWrite("==================" & @CRLF)
ConsoleWrite(_DateFormat("13:11:02", "h:mm tt") & @CRLF)
ConsoleWrite("==================" & @CRLF)


Func _DateFormat($DateTime, $sFormat)
    If StringRegExp($DateTime, "\d{4}/\d{1,2}/\d{1,2}") = 0 Then $DateTime = _NowCalcDate() & " " & $DateTime
    GUICreate("DateTimePick Set Format", 400, 300)
    Local $idDate = GUICtrlCreateDate($DateTime, 2, 6, 190)
    GUICtrlSendMsg($idDate, 0x1032, 0, $sFormat)
    Return GUICtrlRead($idDate)
EndFunc   ;==>_DateFormat

#cs ; Returns:-
21-Dec-2016
==================
Wed Dec 21, 2016 01:11:02 PM A.D.
==================
1:11 PM
==================
#ce

 

Reply to post #3 - another similar method to remove empty elements in an array.

#include <Array.au3>
#include <StringConstants.au3>

Local $arr[7] = [0, "", "   1  ", " 2 ", "3   ", "   4", "  "]
_ArrayDisplay($arr, "Before")

RemoveEmptyArrayLines($arr)
_ArrayDisplay($arr, "After")


Func RemoveEmptyArrayLines(ByRef $arrLines)
    For $intCount = UBound($arrLines) - 1 To 0 Step -1
        $arrLines[$intCount] = StringStripWS($arrLines[$intCount], $STR_STRIPLEADING + $STR_STRIPTRAILING)
        If $arrLines[$intCount] = "" Then
            _ArrayDelete($arrLines, $intCount)
        EndIf
    Next
EndFunc   ;==>RemoveEmptyArrayLines

 

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

  • Similar Content

    • ur
      By ur
      I have date in the string format as "DD-MM-YYYY".
       
      I need to get yesterday's date from it.
      I tried converting this from _DateTimeFormat but not working.
      Is there any direct UDF available to get this.?
    • ur
      By ur
      In some systems the date is displayed in the Taskbar as
      10-01-2018
      And in some systems it is like, 1/10/2018
      And in some it is, 10-Jan-2018
      And in different formats.
       
      I wrote my code to convert 1/10/2018 to 10-01-2018
      Func TodaysDate() $NewDate = _DateTimeFormat(_NowCalcDate(),1) $Array = StringSplit( $NewDate , ',' ) _ArrayDelete($Array, 0) _ArrayDelete($Array, 0) $Array1 = StringSplit($Array[0],' ') RemoveEmptyArrayLines($Array1) ;Will return the present day's date with format dd-mmm-yyyy ;$Date = StringStripWS($Array1[2]&"-"&StringLeft($Array1[1], 3)&"-"&$Array[1], $STR_STRIPALL) $Date = StringStripWS($Array1[2]&"-"&changeDateformat(StringLeft($Array1[1], 3))&"-"&$Array[1], $STR_STRIPALL) return $Date EndFunc Func RemoveEmptyArrayLines(ByRef $arrLines) $intCount = 1 While $intCount < UBound($arrLines) $arrLines[$intCount] = StringStripWS($arrLines[$intCount],$STR_STRIPLEADING + $STR_STRIPTRAILING) If StringLen($arrLines[$intCount])=0 Then _ArrayDelete($arrLines, $intCount) $intCount = $intCount - 1 EndIf $intCount = $intCount + 1 WEnd EndFunc ;To convert mmm to mm format Func changeDateformat($sText) Local $sMsg = StringStripWS($sText, $STR_STRIPALL) Switch $sMsg Case "Jan" $sMsg = "01" Case "Feb" $sMsg = "02" Case "Mar" $sMsg = "03" Case "Apr" $sMsg = "04" Case "May" $sMsg = "05" Case "Jun" $sMsg = "06" Case "Jul" $sMsg = "07" Case "Aug" $sMsg = "08" Case "Sep" $sMsg = "09" Case "Oct" $sMsg = "10" Case "Nov" $sMsg = "11" Case "Dec" $sMsg = "12" EndSwitch return $sMsg EndFunc But again it will work on machines only with 1/10/2018.
      Is there any direct function which will give only in the format 10-01-2018, whatever the system settings is.?
      I tried all the arguments for _DateTimeFormat, but showing machine dependent settings only.
      Google given these two links in AutoIT but these are also for specific formats only,like mine.
       
      Please suggest.
    • kawliga751
      By kawliga751
      I have established how to get tomorrows date however I can't seem to figure out how to format the date into my required format of MM/DD/YYYY.  
      I have this:
      $today=_DateToDayValue(@YEAR,@MON,@MDAY) Dim $Y, $M, $D $tomorrow=_DayValueToDate($today+1, $Y, $M, $D) The _DateToDayValue seems to have a fixed format.  I've also tried a :
      $vardate = _DateAdd( 'd',1, _NowCalcDate()) with a "StringSplit" and/or StringFormat but the _NowCalcDate seems to also be fixed to a YYYY/MM/DD format.
      I'm sure I'm missing something simple but am at a loss.
      Any help would be MUCH appreciated
       
       
    • ur
      By ur
      To get the current time stamp, I got the below code.
      #include <Date.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #RequireAdmin ; Under Vista the Windows API "SetSystemTime" may be rejected due to system security $td = _Date_Time_GetSystemTime() $td = _Date_Time_SystemTimeToDateTimeStr($td) $td = StringReplace($td, " ", "_") $td = StringReplace($td, ":", "_") MsgBox(0,"",$td) But it is not giving the date or time of the timezone where the system is there.
      Please suggestt
    • MrCheese
      By MrCheese
      Hi guys,
       
      I have some code; which doesn't quite work.
      $dateStop = "11302015" $tDate = _Date_Time_GetSystemTime() $currentdatestring = _Date_Time_SystemTimeToDateStr($tDate) If $currentdatestring >= $dateStop Then MsgBox(0, "Expired", "Script has expired.") Exit EndIf  
      Basically, I want to essentially put an expiry date on the script.
       
      Where in the example above, it is 30th November 2015.
       
      Sometimes it works, sometimes it doesn't. 
      I may have the string wrong, I may have the code all wrong.
       
      I thought about using the macro @mon @year etc but it gets quite conditional.
      i.e. if its 2015, but October, or if its 2015 and Dec, or if its March 2016 etc.
       
      But simplicity is great. Any help would be awesome.