excel vba - 如何获取日期的日期名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40408812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
excel vba - How to get the Day Name of Date?
提问by bigbryan
I have a small code that gets the Day Name of the Dates listed in Excel. But the problem is, it's not getting the right Day Name(e.g. Tuesday).
我有一个小代码,用于获取 Excel 中列出的日期的日期名称。但问题是,它没有得到正确的日期名称(例如星期二)。
For Example:
例如:
sDayName = Format(Day(11/1/2016), "dddd")
Then the output produces an incorrect Day Name which is:
然后输出产生不正确的日期名称,即:
sDayName = "Sunday"
when it's supposed to be "Tuesday". Thanks for the help guys.
什么时候应该是“星期二”。谢谢你们的帮助。
回答by cyberponk
For your specific request, assuming your date string is formatted as your regional setings:
对于您的特定请求,假设您的日期字符串格式为您的区域设置:
sDayName = Format("11/01/2016", "dddd")
If you want the weekday for today:
如果您想要今天的工作日:
sDayName = Format(Date, "dddd")
If you want the weekday for any date in any regional settings
如果您想要任何区域设置中任何日期的工作日
sDayName = Format(DateSerial(2016, 11, 4), "dddd")
'change 2016, 11, 4 for your year, month, day values
:)
:)
回答by kadrleyn
WEEKDAY Function can be used on this topic and in Vba Codes . For example:
WEEKDAY 函数可用于本主题和 Vba 代码。例如:
Label2.Caption = WeekdayName(Weekday(TextBox2, 0), False, 0)
I used this function in when I created userform about date entry to active cell.
我在创建关于活动单元格日期输入的用户表单时使用了这个函数。

