Jump to content
ur

Date in particular format

Recommended Posts

ur

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
ur

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
ur

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
Malkey

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

    • Siryx
      By Siryx
      So what i need is a function that I pass a starting date into and it counts up to the current date. Example:
      2009-05-30
      2009-05-31
      2009-06-01
      ...
      2018-06-18
      How would I do that? I found the Date.au3 in the includes but I can't find anything close to a time object like I'm used to working with in Java. I just need some $date = setDate(2009-05-30) and from there I could just add a day every time. I need this to be in the very format I stated earlier and from what I can see everytime related to date and time is automatically changed to my german locale.
      €: I already tried setDate but instead it changed my PCs clock. Not quite what i was looking for  
    • nacerbaaziz
      By nacerbaaziz

      Hello
      I have a question please
      How to show Islamic date in Autoit
      I mean
      for example
      Ramadan month
      And moharam month ... etc
      I hope my question is clear for you
      Thanks in advance
    • Jury
      By Jury
      So I get:
      $aMatch[0] Date(1499295600000)   this is supposed to be 06 Jul 2017
      $aMatch[0] Date(1483574400000)  this is supposed to be 05 Jan 2017
      does anybody know the formula to calculate the date from the large number?  It doesn't seem to be anything to do with DateToDayValue.
      thanks in advance.
    • litlmike
      By litlmike
      I am taking a Date & Time and splitting them using _DateTimeSplit, so that I can create a filename in the format: YYYYMMDD_HHMMSS.jpg.  The only issue is that if the month, or day, is a two-digit number that begins with a zero (so less than 10), then the month, or day, is formatted as a single-digit number.  Suggestions on how to accomplish this?
      Example:
      20180101 becomes 2018, 1, 1
      20180303 becomes 2018, 3, 3
      20180505 becomes 2018, 5, 5
      20180606 becomes 2018, 6, 6
      20180909 becomes 2018, 9, 9
      What I want:
      20180101 becomes 2018, 01, 01
      20180303 becomes 2018, 03, 03
      20180505 becomes 2018, 05, 05
      20180606 becomes 2018, 06, 06
      20180909 becomes 2018, 09, 09
      Func _CreateFileNameFromDateTimeFormat($sDateThatWasChangedIntoDateTimeFormat = "2018/01/02 10:29:39") Local $aMyDate, $aMyTime, $sNewImageFileName _DateTimeSplit($sDateThatWasChangedIntoDateTimeFormat, $aMyDate, $aMyTime) _ArrayDisplay($aMyDate) _ArrayDisplay($aMyTime) ;This will become file name $sNewImageFileName = $aMyDate[1] & $aMyDate[2] & $aMyDate[3] & "_" & $aMyTime[1] & $aMyTime[2] & $aMyTime[3] & ".jpg" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sNewImageFileName = ' & $sNewImageFileName & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console EndFunc ;==>_CreateFileNameFromDateTimeFormat  
       
    • Shedunn
      By Shedunn
      I've been looking through some different functions(not sure if that's the right word) to get the Date and time and I can't find one that doesn't include "/ " or ":" in what is returned.

      I'm trying to create a file name with this format "QuickConfig_MMDDYYYY_HHMM"  (doesn't matter if 24 or 12hr format)
      #include <Date.au3> Func _SaveConfig MouseClick('primary',155, 46, 1, 0) ;clicks save as WinWait('Save bluePRINT Configuration As...','', 4) MouseClick('primary',632, 47, 1, 0) ;clicks the file path bar $Documents = @MyDocumentsDir $FilePath = $Documents & '\BP3 Configs' Send($FilePath) Send('{ENTER}') MouseClick('primary',166, 580, 1, 0) ;clicks file name bar $FileName = 'QuickConfig_' & _NowDate & '_' & _NowTime Send($FileName) Send('{ENTER}') EndFunc  
       
      I'm wondering if there is a function somewhere to do this?
×