Jump to content

opposite of _date_time_systemtimetodatestr() ?


Recommended Posts

I need a way of getting the opposite of the function _date_time_systemtimetodatestr()

which this function given yyyy/mm/dd returns "February 23, 2013" as per my PC setup.

That's the date I use in ListView column. Problem is now to edit that item I need to take

that date and convert it back to yyyy/mm/dd . So I need _date_time_datestrtosystemtime()

to get the opposite to use with GUICtrlCreateDate()

Yes I know how to manually break apart the string and create yyyy/mm/dd but I don't want

to assume that other users will have the date format as me. I need a generic way to get the results.

Link to comment
Share on other sites

  • Moderators

Garp99HasSpoken,

I suggest using Malkey's _Date_Time_Convert UDF - it converts to and from any format you care to use. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Does anyone know how to get the LOCALE_SLONGDATEFORMAT from a user's PC? From Registry or program call.

There are differences between Long Date Format for English-Canada, English-United_States and English-United_Kingdom

I am hoping to find a method of getting the layout which the user prefers, in a format like

MMMM dd, yyyy

MMMM d, yyyy

MMMM-dd-yy

d-MMM-yy

dddd, MMMM dd, yyyy

dddd, dd MMM, yyyy

dd MMMM, yyyy

dd MMMM yyyy

d MMMM yyyy

From there I can convert back to GUI Date/Time Picker format yyyy/mm/dd

Link to comment
Share on other sites

@Malkey, you misunderstood my needs, as well as the same reason your date convert function would not work for my needs.

@kylomas, thanks. I should indeed take a better look at what's available there.

Anyways, wrote a function to handle my needs for any English or French countries, regardless of user's preference.

Check out this example program.

#include <Date.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>

GUICreate("Date Examples", 300, 400)

Global $nx=16
Global $examples[ $nx ] = [ _
        "March 4, 2012", "4-March-12", "January-22-13", "14-May-12", "December 23, 2012", _
        "Sunday, February 24, 2013", "Sunday, 24 February, 2013", "24 February, 2013", "29 January 2003", "4 November 2012", _
        "Sunday, February 3, 2013", "Sunday, 3 February, 2013", _
        "01-Jul-69", "11-Jun-14", "31-Oct-03", "3 May 11" ]

$cListView = GUICtrlCreateListView("Long Date                                 |YY/MM/DD ", 10, 10, 280, 380)

For $i=0 To $nx-1
    GUICtrlCreateListViewItem($examples[$i] & "|" & LongDateToString($examples[$i]), $cListView)
Next
_GUICtrlListView_SetColumnWidth($cListView, $i, $LVSCW_AUTOSIZE_USEHEADER)
GUISetState()


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
Exit

; Convert long month string to number
Func LongMonthToNum($mmmm)
    Local $i, $mm = 0
    If (StringRight(@OSLang, 2) = "0c") Then        ; French countries
        Local $months[12] = [ "jan", "fev", "mar", "avr", "mai", "jui", "jul", "aou", "sep", "oct", "nov", "dec" ]
;    ElseIf (StringRight(@OSLang, 2) = "09") Then        ; English countries
    Else        ; Handle all others
        Local $months[12] = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
    EndIf

    For $i=0 To 11
        If (StringLeft($mmmm, 3) = $months[$i]) Then
            $mm = $i + 1
            ExitLoop
        EndIf
    Next
    Return $mm
EndFunc   ;==>LongMonthToNum

; Convert user's Long Date format to yyyy/mm/dd for GUI date/time picker
Func LongDateToString($dt)
    Local $ymd, $lm, $yy = 0, $mm = 0, $dd = 0

    ; Check for ddd, MMMM d, yyyy"
    If (StringRegExp($dt, "\w*, \w* \d{1,2}, \d{4}")) Then
        $yy = StringRegExpReplace($dt, "\w*, (\w*) (\d{1,2}), (\d{4})", "\3")
        $dd = StringRegExpReplace($dt, "\w*, (\w*) (\d{1,2}), (\d{4})", "\2")
        $lm = StringRegExpReplace($dt, "\w*, (\w*) (\d{1,2}), (\d{4})", "\1")
        $mm = LongMonthToNum($lm)
ConsoleWrite("Case 1: " & $dt & " --> lm (" & $lm & ") " & $yy & "-" & $mm & "-" &  $dd & @CRLF)

    ; Check for "MMMM dd, yyyy" or "MMMM d, yyyy"
    ElseIf (StringRegExp($dt, "\w* \d{1,2}, \d{4}")) Then
        $yy = StringRegExpReplace($dt, "(\w*) (\d{1,2}), (\d{4})", "\3")
        $dd = StringRegExpReplace($dt, "(\w*) (\d{1,2}), (\d{4})", "\2")
        $lm = StringRegExpReplace($dt, "(\w*) (\d{1,2}), (\d{4})", "\1")
        $mm = LongMonthToNum($lm)
ConsoleWrite("Case 2: " & $dt & " --> lm (" & $lm & ") " & $yy & "-" & $mm & "-" &  $dd & @CRLF)

    ; Check for "MMMM-dd-yy"
    ElseIf (StringRegExp($dt, "\w*-\d\d-\d\d")) Then
        $yy = StringRegExpReplace($dt, "(\w*)-(\d\d)-(\d\d)", "\3")
        $dd = StringRegExpReplace($dt, "(\w*)-(\d\d)-(\d\d)", "\2")
        $lm = StringRegExpReplace($dt, "(\w*)-(\d\d)-(\d\d)", "\1")
        $mm = LongMonthToNum($lm)
ConsoleWrite("Case 3: " & $dt & " --> lm (" & $lm & ") " & $yy & "-" & $mm & "-" &  $dd & @CRLF)

    ; Check for "dddd, dd MMMM, yyyy"
    ElseIf (StringRegExp($dt, "\w*, \d{1,2} \w*,? \d{4}")) Then
        $yy = StringRegExpReplace($dt, "\w*, (\d{1,2}) (\w*),? (\d{4})", "\3")
        $dd = StringRegExpReplace($dt, "\w*, (\d{1,2}) (\w*),? (\d{4})", "\1")
        $lm = StringRegExpReplace($dt, "\w*, (\d{1,2}) (\w*),? (\d{4})", "\2")
        $mm = LongMonthToNum($lm)
ConsoleWrite("Case 4: " & $dt & " --> lm (" & $lm & ") " & $yy & "-" & $mm & "-" &  $dd & @CRLF)

    ; Check for "dd MMMM, yyyy" or "d MMMM yyyy" or "dd MMMM yyyy"
    ElseIf (StringRegExp($dt, "\d{1,2} \w*,? \d{4}")) Then
        $yy = StringRegExpReplace($dt, "(\d{1,2}) (\w*),? (\d{4})", "\3")
        $dd = StringRegExpReplace($dt, "(\d{1,2}) (\w*),? (\d{4})", "\1")
        $lm = StringRegExpReplace($dt, "(\d{1,2}) (\w*),? (\d{4})", "\2")
        $mm = LongMonthToNum($lm)
ConsoleWrite("Case 5: " & $dt & " --> lm (" & $lm & ") " & $yy & "-" & $mm & "-" &  $dd & @CRLF)

    ; Check for "d-MMMM-yy" or "dd-MMMM-yy"
    ElseIf (StringRegExp($dt, "\d{1,2}-\w*-\d\d")) Then
        $yy = StringRegExpReplace($dt, "(\d{1,2})-(\w*)-(\d\d)", "\3")
        $dd = StringRegExpReplace($dt, "(\d{1,2})-(\w*)-(\d\d)", "\1")
        $lm = StringRegExpReplace($dt, "(\d{1,2})-(\w*)-(\d\d)", "\2")
        $mm = LongMonthToNum($lm)
ConsoleWrite("Case 6: " & $dt & " --> lm (" & $lm & ") " & $yy & "-" & $mm & "-" &  $dd & @CRLF)

    ; Check for d MMMM yy" or "dd MMMM yy" (mainly French countries)
    ElseIf (StringRegExp($dt, "\d{1,2} \w* \d\d")) Then
        $yy = StringRegExpReplace($dt, "(\d{1,2}) (\w*) (\d\d)", "\3")
        $dd = StringRegExpReplace($dt, "(\d{1,2}) (\w*) (\d\d)", "\1")
        $lm = StringRegExpReplace($dt, "(\d{1,2}) (\w*) (\d\d)", "\2")
        $mm = LongMonthToNum($lm)
ConsoleWrite("Case 7 " & $dt & " --> lm (" & $lm & ") " & $yy & "-" & $mm & "-" &  $dd & @CRLF)

    EndIf

    If ($yy > 33 And $yy < 100) Then $yy += 1900
    If ($yy < 34) Then $yy += 2000
    $ymd = StringFormat("%04d/%02d/%02d", $yy, $mm, $dd)
    Return $ymd
EndFunc   ;==>LongDateToString
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...