+ Reply to Thread
Results 1 to 11 of 11

convert string to date format of "d-mmm-yyyy"

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    convert string to date format of "d-mmm-yyyy"

    I am asking the user to input a date........it gets input as a string.

    I then convert the string to a date using the function

    "Cdate(expression)"

    I then try and convert the date to a format of "d-mmm-yyyy" but it does not work.

    lets say user inputs the date i want user to input is december 9 2011 and so they input "9/12/2011"

    i use cdate to convert to date

    i then use

    Please Login or Register  to view this content.
    so the date now should show up as 9-Dec-2011

    but it doesnt it shows up as "12/9/11"


    any thoughts?



    i am attaching an example file

    what i found out ........is that if i declare the variable as a date.........it does not work...........if i dont declare the variable then it works.............any thoughts.
    Attached Files Attached Files
    Last edited by welchs101; 12-19-2011 at 08:42 AM.

  2. #2
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: convert string to date format of "d-mmm-yyyy"

    anyone got any ideas?

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: convert string to date format of "d-mmm-yyyy"

    Hi

    Have you had a look at the region setting for the PC? Your profile doesn't say where you are located.

    rylo

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: convert string to date format of "d-mmm-yyyy"

    i am not sure i understand........i am located in the US........

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: convert string to date format of "d-mmm-yyyy"

    Hi

    Go to the Control Panel, Regional and language options, regional settings, and not your current selection. Then change it to English(Australian) and try you code again. If it works, the I'd say it has something to do with how CDate is converting your string to a date.

    Make sure you return your regional settings again.

    rylo

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: convert string to date format of "d-mmm-yyyy"

    ok, here is the thing.

    I am writing some code for a coworker who is another country.

    I ask the user to input a date in th format "dd-mm-yyyy" ......i use an input box for this.

    i try to convert this date string into a date ........and into a specific date format.

    i am pulling out my hair.........any suggestions.............

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: convert string to date format of "d-mmm-yyyy"

    ok, so i have been thinking about this a little and i think this is my problem........but i will leave it to you guys......

    bu here is whati am thinking.

    I am asking the user via an input box to input a date in a specified format of "d-mm-yyyy". So if the user puts in 9/12/2011 this is supposed to be December 9th 2011 but my excel interprets this date as Sept,12 2011........

    i need to ask the user to input a date............thing is i need to know what "format" they input the date in.

  8. #8
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: convert string to date format of "d-mmm-yyyy"

    Hi Welchs

    Your code DIMs sets test_date as a Date. So, it is a value, not a string. You try to set it as a string using the Format command, but it hangs on to the date value only (not the text format).

    So, either Dim test_date as a string, or move the Format command to the MsgBox command:-

    Please Login or Register  to view this content.
    Regards, Rob.

  9. #9
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: convert string to date format of "d-mmm-yyyy"

    (this thing is only letting me do a quick reply .....so )

    This is what i came up with........see code below(I could not put it into the format i know i am supposed to because this thing is only letting me do a quick reply).

    Question:
    What i have works but ONLY IF the format i put it in is "dd-mm-yyyy"..........if i try to use "d-mmm-yyyy" my code does not work.

    Any ideas?

    I am trying to take input from the user "specifying" the date format they are supposed to input the data in.......i then take this date/data and subtract off another date and compare the delta




    Dim fl_name As String

    fl_name = ThisWorkbook.Name
    Dim test_date1_string As String
    Dim test_date1_date As Date
    Dim test_date3_date As Date
    Dim user_input_date As Variant
    Dim junk1 As Date



    Set ws = Workbooks(fl_name).Worksheets("Sheet1")

    Dim StrtDate As String
    StrtDate = Application.InputBox("inptu date as dd-mm-yyy")
    If StrtDate = "" Then
    date_error = True
    ElseIf StrtDate = "False" Then
    date_error = True
    ElseIf StrtDate <> "" Then
    'user_input_date = CDate(StrtDate)
    StrtDate = Format((StrtDate), "dd-mm-yyyy")
    user_input_date = DateValue(StrtDate)
    'user_input_date = Format(CDate(StrtDate), Std_date_format)
    date_error = False
    End If



    MsgBox (user_input_date)

    junk1 = DateValue("12 / 8 / 2011")

    MsgBox (user_input_date - junk1)

  10. #10
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: convert string to date format of "d-mmm-yyyy"

    am i doing this the best way............to be honest i came along this soln by accident.......i was just putting values and it happen to work......but i am not sure why.........

  11. #11
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: convert string to date format of "d-mmm-yyyy"

    since i have a lot of questions about why this code is working.............i think i probably should not use it.

    Instead i am thinking of asking 3 questions:
    1) input year
    2) input month
    3) input day of month..........

    i think i can then use these three to get the right date no matter what...........

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1