简体   繁体   English

如何将VBA TextBox格式化为长日期

[英]How to Format VBA TextBox To Long Date

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'. 我有一个日期显示文本框,以这种格式显示日期:17/04/2012但我想将其格式化为“2012年4月17日”。 I tried this code 我试过这段代码

UserForm1.txtDate = Format(Long Date)

which I am getting syntax error from compiler.Can you please let me know how I can do it? 我从编译器得到语法错误。你能告诉我怎么做吗? Thanks 谢谢

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, eg Format$(yourdate, “dd/mm/yyyy”) 在美国系统中,主要是完成它,例如Format$(yourdate, “dd/mm/yyyy”)
  • Where as European systems, eg Format$(yourdate, “short date”) 欧洲系统的位置,例如Format$(yourdate, “short date”)

If we look at explicitly defining your format, you are formulating your regional settings! 如果我们看一下明确定义您的格式,那么您正在制定区域设置!

Good eg short date won't always be mm/dd/yyyy but could be dd/mm/yyyy. 好的, 例如短日期不会总是mm / dd / yyyy,但可能是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. 当我们第一次遇到这个时,我们有一些制作VBA应用程序并将其发送到另一个地方,在那里他们拥有自己的本地化系统日期格式。 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. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM