How to Format VBA TextBox To Long Date

前端 未结 1 532
借酒劲吻你
借酒劲吻你 2021-01-17 04:07

I have a textbox for date display which shows the date in this format: 17/04/2012 but I would like to format it to shows up like \'April 17, 2012\'. I tried this code

<
1条回答
  •  孤城傲影
    2021-01-17 04:27

    there have been couple of posts on this. Culprit seems to be your system date format. Use of short, medium, long formats will change the result when the system settings change.

    • In US systems mainly it's done as, e.g. Format$(yourdate, “dd/mm/yyyy”)
    • Where as European systems, e.g. Format$(yourdate, “short date”)

    If we look at explicitly defining your format, you are formulating your regional settings!

    Good e.g. short date won’t always be mm/dd/yyyy but could be dd/mm/yyyy. That’s the key advantage over actually specifying the format. Simply you can change how you want your date to look like instead of depending on the system (with or without your knowledge).

    When we first encountered this, we had some making the VBA apps and sending it over to another in some other place where they have own localized system date format. Your phone rings... ;)

    • Short/Long Date: Display a date according to your system’s long date format.
    • Medium Date: Display a date using the medium date format appropriate for the language version of the host application.

    In your case you could use,

    UserForm1.txtDate.Value = Format$(Date,"mmmm dd, yyyy")
    

    Or to be super-safe,

    Dim dtDate as Date
        dtDate = DateSerial(Year(Date), Month(Date), Day(Date)) 
        UserForm1.txtDate.Value = Format$(dtDate, "mmmm dd, yyyy") 
    

    Hope that helps.

    • References

    0 讨论(0)
提交回复
热议问题