如何将VBA文本框格式化为长日期 [英] How to Format VBA TextBox To Long Date

查看:596
本文介绍了如何将VBA文本框格式化为长日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期显示的文本框,显示了以下格式的日期:17/04/2012,但我想将其格式化为2012年4月17日。我试过这个代码

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

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

推荐答案

此处有几个帖子。 Culprit似乎是您的系统日期格式。使用短,中,长格式会在系统设置更改时更改结果。

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.


  • 在美国系统中,主要是以例如格式$(yourdate,dd / mm / yyyy)

  • 格式$(yourdate,short date)

  • 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!

例如短日期并不总是mm / dd / yyyy,但可以是dd / mm / yyyy。这是实际指定格式的关键优势。只要你可以改变你的日期,而不是依赖于系统(有或没有你的知识)。

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).

当我们第一次遇到这个时,我们有一些制作VBA应用程序并将其发送到另一个其他具有本地化系统日期格式的位置。您的手机响起...;)

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... ;)


  • 短/长日期:根据系统的长日期格式显示日期。

  • 中日期:使用适合于主机应用程序的语言版本的中等日期格式显示日期。

您可以使用

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

或者要超级安全,

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

希望有所帮助

  • References

这篇关于如何将VBA文本框格式化为长日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆