如何在MS-Access中获取日期名称

huangapple go评论49阅读模式
英文:

How to get date name in MS-Access

问题

我有3条记录:

  • day_birth,类型为数字(1-31);
  • month_birth,类型为短文本("January");
  • year_birth,类型为数字。

在计算字段中如何获取完整的出生日期(星期几)?

我尝试了以下表达式:

WeekdayName(Weekday(DateSerial(2003, 1, 1), 2))

但它不起作用。在我的记录字段中,我得到了 #Type!。

英文:

I have 3 record:
day_birth of type Number (1-31);
month_birth of type Short Text ("January");
year_birth of type Number.

How to get in calculated field, full birthday day name from this day?

I tried this one expression:

WeekdayName(Weekday(DateSerial(2003;1;1);2))

But it doesn't work. I get #Type! in my record field.

答案1

得分: 2

以下是翻译好的部分:

  • 使用此代码:
    ? format(dateserial(2003,1,1),"ddd")
    Wed
    或者这个:
    ? format(dateserial(2003,1,1),"dddd")
    Wednesday

  • 但是,您暗示/陈述/暗示/提到/指出您的输入月份是长文本月份一月(短文本将是Jan)。

  • 所以,我们需要考虑这一点。

  • 然而,您建议为什么出生日期(DOB)不只是存储为一个简单的日期时间列呢?有了这样的列,您就可以自由地将其拆分为三列用于报告等,但始终具有一个简单的日期列。

  • 但是,您现在所拥有的可能不是您的错。

  • 所以,然后:
    Dim strTestMonth As String
    Dim intTestYear As Integer
    Dim intTestDay As Integer

    strTestMonth = "January"
    intTestYear = 2003
    intTestDay = 1

    Dim strTestDate As String
    Dim strDateFormat As String

    strDateFormat = "mmmm/dd/yyyy"

    strTestDate = strTestMonth & "/" & intTestDay & "/" & intTestYear

    Dim dtDate As Date

    dtDate = Format(strTestDate, strDateFormat)

    ' 获取星期几的数字
    Debug.Print Weekday(dtDate)

    ' 获取星期几的文本
    Debug.Print "Short day of week = " & Format(dtDate, "ddd")

    Debug.Print "Long day of week " & Format(dtDate, "dddd")

  • 输出:
    4
    Short day of week = Wed
    Long day of week Wednesday

  • 所以,方法是:
    将字符串转换为内部日期,一旦完成,您可以以任何您想要的格式输出日期,包括使用工作日函数,甚至可以返回短日格式(wed)或较长的日期格式(Wednesday)。

  • 不清楚您是否需要在表单上显示星期几,还是在报告中或其他地方。因此,非常重要的问题一直是您需要此表达式的时间/地点/方式/内容是什么时候。

  • 例如,您可以将此函数放在标准代码模块中(而不是表单代码模块):
    Public Function GetDayOfWeek(strMonth As String, _
    intDay As Integer, _
    intYear As Integer) As String

      Dim strDateFormat As String
      Dim dtDate As Date
    
      strDateFormat = "mmmm/dd/yyyy"
    
      dtDate = Format(strMonth & "/" & intDay & "/" & intYear, strDateFormat)
    
      GetDayOfWeek = Format(dtDate, "dddd")
    

    End Function

  • 现在,每当您需要以“文本”方式显示星期几时,甚至可以在表单上的控件上使用此函数:
    =(GetDayOfWeek([MonthField],[dayField], [YearField]))

  • 将上述代码放入标准代码模块中(而不是表单代码模块),然后在代码中,甚至作为表单上控件的表达式,您可以传递这3个值,它将以长文本格式返回/显示星期几。

英文:

Well, you can use this:

? format(dateserial(2003,1,1),"ddd")
Wed

or even this:

? format(dateserial(2003,1,1),"dddd")
Wednesday

However, you suggest/state/hint/imply/note that your input month is a LONG TEXT month of January. (short would be Jan).

So, we need to take into account that fact.

(but, why oh why oh why oh why oh why was the DOB not just stored as a simple single datetime column? With such a column, then you are 100% free to break it apart into 3 columns for reporting etc., but always have one simple date column

However, you have what you have, probably not your fault.

So, then:

Dim strTestMonth        As String
Dim intTestYear         As Integer
Dim intTestDay          As Integer

strTestMonth = "January"
intTestYear = 2003
intTestDay = 1

Dim strTestDate         As String
Dim strDateFormat       As String

strDateFormat = "mmmm/dd/yyyy"

strTestDate = strTestMonth & "/" & intTestDay & "/" & intTestYear

Dim dtDate              As Date

dtDate = Format(strTestDate, strDateFormat)


' get day of week as number
Debug.Print Weekday(dtDate)

' get day of week as text


Debug.Print "Short day of week = " & Format(dtDate, "ddd")

Debug.Print "Long day of week " & Format(dtDate, "dddd")

Output:

 4 
 Short day of week = Wed
 Long day of week Wednesday

So, approach:

Convert the string into a internal date, and once done, then you are free to spit out the date in any format you want, including use of weekday function, or even format which can return a short day format (wed), or a longer date format (Wednesday).

It not clear if you need this "display" of the week day on the form, or in a report or whatever. So as always the VERY important issue becomes the when/where/how/what point in time you need this expression.

So, you could for example place this function in a standard code module: (not forms code module)

Public Function GetDayOfWeek(strMonth As String, _
                            intDay As Integer, _
                            intYear As Integer) As String

    Dim strDateFormat   As String
    Dim dtDate          As Date
        
    strDateFormat = "mmmm/dd/yyyy"
    
    dtDate = Format(strMonth & "/" & intDay & "/" & intYear, strDateFormat)
    
    GetDayOfWeek = Format(dtDate, "dddd")
    

End Function

So, now when ever and "where" ever you need to display the weekday as "text", then you can do this for even a control on that form:

  =(GetDayOfWeek([MonthField],[dayField], [YearField]))

So, place the above code in a standard code module (not forms code module), and then in code, or even as a expression on a control in a form, you can pass the 3 values, and it will return/display the day of week in long text format.

答案2

得分: 0

使用DateValue和一个简单的查询:

Select 
    *, 
    DateValue(Str([year_birth]) & " " & [month_birth] & Str([day_birth])) As BirthDate,
    WeekdayName(Weekday(DateValue(Str([year_birth]) & " " & [month_birth] & Str([day_birth]), 2), 2) As BirthWeekday
From 
    YourTable
英文:

Use DateValue and a simple query:

Select 
    *, 
    DateValue(Str([year_birth]) & " " & [month_birth] & Str([day_birth])) As BirthDate,
    WeekdayName(Weekday(DateValue(Str([year_birth]) & " " & [month_birth] & Str([day_birth])), 2), 2) As BirthWeekday
From 
    YourTable

huangapple
  • 本文由 发表于 2023年2月19日 17:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定