如何:从特定日期中提取星期几

利用 .NET,可以很容易地确定某个特定日期是星期几,以及显示某个特定日期的本地化星期几名称。 指示与特定日期相对应的星期几的枚举值可以从 DayOfWeekDayOfWeek 属性中获取。 与此不同的是,检索星期几名称是一项格式化操作,可通过调用格式化方法来执行,例如日期和时间值的 ToString 方法或 String.Format 方法。 本文演示如何执行这些格式化操作。

提取指示星期几的数字

  1. 使用静态 DateTime.ParseDateTimeOffset.Parse 方法将日期的字符串表示形式转换为 DateTimeDateTimeOffset 值。

  2. 使用 DateTime.DayOfWeekDateTimeOffset.DayOfWeek 属性检索指示星期几的 DayOfWeek 值。

  3. 如有必要,可将 DayOfWeek 值强制转换(在 C# 中)或转换(在 Visual Basic 中)为整数。

下面的示例将显示一个整数,用于表示特定日期的星期几:

using System;

public class Example
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2008, 6, 11);
      Console.WriteLine((int) dateValue.DayOfWeek);
   }
}
// The example displays the following output:
//       3
Module Example
    Public Sub Main()
        Dim dateValue As Date = #6/11/2008#
        Console.WriteLine(dateValue.DayOfWeek)
    End Sub
End Module
' The example displays the following output:
'    3

提取缩写的工作日名称

  1. 使用静态 DateTime.ParseDateTimeOffset.Parse 方法将日期的字符串表示形式转换为 DateTimeDateTimeOffset 值。

  2. 你可以提取当前区域性或特定区域性的缩写的星期几名称:

    1. 若要提取当前区域性的缩写的星期几名称,请调用日期和时间值的 DateTime.ToString(String)DateTimeOffset.ToString(String) 实例方法,并以 format 参数的形式传递字符串 ddd。 下面的示例演示 ToString(String) 方法的调用:

      using System;
      
      public class Example
      {
         public static void Main()
         {
            DateTime dateValue = new DateTime(2008, 6, 11);
            Console.WriteLine(dateValue.ToString("ddd"));
         }
      }
      // The example displays the following output:
       //       Wed
      
      Module Example
          Public Sub Main()
              Dim dateValue As Date = #6/11/2008#
              Console.WriteLine(dateValue.ToString("ddd"))
          End Sub
      End Module
      ' The example displays the following output:
      '       Wed
      
    2. 若要提取特定区域性的缩写的星期几名称,请调用日期和时间值的 DateTime.ToString(String, IFormatProvider)DateTimeOffset.ToString(String, IFormatProvider) 实例方法。 同时以 format 参数形式传递字符串 ddd。 以 CultureInfo 参数的形式传递表示要检索其星期几名称的区域性的 DateTimeFormatInfoprovider 对象。 下面的代码阐释如何使用表示 fr-FR 区域性的 ToString(String, IFormatProvider) 对象调用 CultureInfo 方法:

      using System;
      using System.Globalization;
      
      public class Example
      {
         public static void Main()
         {
            DateTime dateValue = new DateTime(2008, 6, 11);
            Console.WriteLine(dateValue.ToString("ddd",
                              new CultureInfo("fr-FR")));
         }
      }
      // The example displays the following output:
      //       mer.
      
      Imports System.Globalization
      
      Module Example
          Public Sub Main()
              Dim dateValue As Date = #6/11/2008#
              Console.WriteLine(dateValue.ToString("ddd",
                                New CultureInfo("fr-FR")))
          End Sub
      End Module
      ' The example displays the following output:
      '       mer.
      

提取完整的工作日名称

  1. 使用静态 DateTime.ParseDateTimeOffset.Parse 方法将日期的字符串表示形式转换为 DateTimeDateTimeOffset 值。

  2. 你可以提取当前区域性或特定区域性的完整的星期几名称:

    1. 若要提取当前区域性的缩写的星期几名称,请调用日期和时间值的 DateTime.ToString(String)DateTimeOffset.ToString(String) 实例方法,并以 format 参数的形式传递字符串 dddd。 下面的示例演示 ToString(String) 方法的调用:

      using System;
      
      public class Example
      {
         public static void Main()
         {
            DateTime dateValue = new DateTime(2008, 6, 11);
            Console.WriteLine(dateValue.ToString("dddd"));
         }
      }
      // The example displays the following output:
      //       Wednesday
      
      Module Example
          Public Sub Main()
              Dim dateValue As Date = #6/11/2008#
              Console.WriteLine(dateValue.ToString("dddd"))
          End Sub
      End Module
      ' The example displays the following output:
      '       Wednesday
      
    2. 若要提取特定区域性的星期几名称,请调用日期和时间值的 DateTime.ToString(String, IFormatProvider)DateTimeOffset.ToString(String, IFormatProvider) 实例方法。 同时以 format 参数形式传递字符串 dddd。 以 CultureInfo 参数的形式传递表示要检索其星期几名称的区域性的 DateTimeFormatInfoprovider 对象。 下面的代码阐释如何使用表示 es-ES 区域性的 ToString(String, IFormatProvider) 对象调用 CultureInfo 方法:

      using System;
      using System.Globalization;
      
      public class Example
      {
         public static void Main()
         {
            DateTime dateValue = new DateTime(2008, 6, 11);
            Console.WriteLine(dateValue.ToString("dddd",
                              new CultureInfo("es-ES")));
         }
      }
      // The example displays the following output:
      //       miércoles.
      
      Imports System.Globalization
      
      Module Example
          Public Sub Main()
              Dim dateValue As Date = #6/11/2008#
              Console.WriteLine(dateValue.ToString("dddd", _
                                New CultureInfo("es-ES")))
          End Sub
      End Module
      ' The example displays the following output:
      '       miércoles.
      

示例

以下示例演示如何调用 DateTime.DayOfWeekDateTimeOffset.DayOfWeek 属性,以检索特定日期中表示星期几的数字。 它还包括调用 DateTime.ToStringDateTimeOffset.ToString 方法来提取缩写的星期几名称和完整的星期几名称。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string dateString = "6/11/2007";
      DateTime dateValue;
      DateTimeOffset dateOffsetValue;

      try
      {
         DateTimeFormatInfo dateTimeFormats;
         // Convert date representation to a date value
         dateValue = DateTime.Parse(dateString, CultureInfo.InvariantCulture);
         dateOffsetValue = new DateTimeOffset(dateValue,
                                      TimeZoneInfo.Local.GetUtcOffset(dateValue));

         // Convert date representation to a number indicating the day of week
         Console.WriteLine((int) dateValue.DayOfWeek);
         Console.WriteLine((int) dateOffsetValue.DayOfWeek);

         // Display abbreviated weekday name using current culture
         Console.WriteLine(dateValue.ToString("ddd"));
         Console.WriteLine(dateOffsetValue.ToString("ddd"));

         // Display full weekday name using current culture
         Console.WriteLine(dateValue.ToString("dddd"));
         Console.WriteLine(dateOffsetValue.ToString("dddd"));

         // Display abbreviated weekday name for de-DE culture
         Console.WriteLine(dateValue.ToString("ddd", new CultureInfo("de-DE")));
         Console.WriteLine(dateOffsetValue.ToString("ddd",
                                                     new CultureInfo("de-DE")));

         // Display abbreviated weekday name with de-DE DateTimeFormatInfo object
         dateTimeFormats = new CultureInfo("de-DE").DateTimeFormat;
         Console.WriteLine(dateValue.ToString("ddd", dateTimeFormats));
         Console.WriteLine(dateOffsetValue.ToString("ddd", dateTimeFormats));

         // Display full weekday name for fr-FR culture
         Console.WriteLine(dateValue.ToString("ddd", new CultureInfo("fr-FR")));
         Console.WriteLine(dateOffsetValue.ToString("ddd",
                                                    new CultureInfo("fr-FR")));

         // Display abbreviated weekday name with fr-FR DateTimeFormatInfo object
         dateTimeFormats = new CultureInfo("fr-FR").DateTimeFormat;
         Console.WriteLine(dateValue.ToString("dddd", dateTimeFormats));
         Console.WriteLine(dateOffsetValue.ToString("dddd", dateTimeFormats));
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert {0} to a date.", dateString);
      }
   }
}
// The example displays the following output:
//       1
//       1
//       Mon
//       Mon
//       Monday
//       Monday
//       Mo
//       Mo
//       Mo
//       Mo
//       lun.
//       lun.
//       lundi
//       lundi
Imports System.Globalization

Module Example
    Public Sub Main()
        Dim dateString As String = "6/11/2007"
        Dim dateValue As Date
        Dim dateOffsetValue As DateTimeOffset

        Try
            Dim dateTimeFormats As DateTimeFormatInfo
            ' Convert date representation to a date value
            dateValue = Date.Parse(dateString, CultureInfo.InvariantCulture)
            dateOffsetValue = New DateTimeOffset(dateValue, _
                                        TimeZoneInfo.Local.GetUtcOffset(dateValue))
            ' Convert date representation to a number indicating the day of week
            Console.WriteLine(dateValue.DayOfWeek)
            Console.WriteLine(dateOffsetValue.DayOfWeek)

            ' Display abbreviated weekday name using current culture
            Console.WriteLine(dateValue.ToString("ddd"))
            Console.WriteLine(dateOffsetValue.ToString("ddd"))

            ' Display full weekday name using current culture
            Console.WriteLine(dateValue.ToString("dddd"))
            Console.WriteLine(dateOffsetValue.ToString("dddd"))

            ' Display abbreviated weekday name for de-DE culture
            Console.WriteLine(dateValue.ToString("ddd", New CultureInfo("de-DE")))
            Console.WriteLine(dateOffsetValue.ToString("ddd", _
                                                       New CultureInfo("de-DE")))

            ' Display abbreviated weekday name with de-DE DateTimeFormatInfo object
            dateTimeFormats = New CultureInfo("de-DE").DateTimeFormat
            Console.WriteLine(dateValue.ToString("ddd", dateTimeFormats))
            Console.WriteLine(dateOffsetValue.ToString("ddd", dateTimeFormats))

            ' Display full weekday name for fr-FR culture
            Console.WriteLine(dateValue.ToString("ddd", New CultureInfo("fr-FR")))
            Console.WriteLine(dateOffsetValue.ToString("ddd", _
                                                       New CultureInfo("fr-FR")))

            ' Display abbreviated weekday name with fr-FR DateTimeFormatInfo object
            dateTimeFormats = New CultureInfo("fr-FR").DateTimeFormat
            Console.WriteLine(dateValue.ToString("dddd", dateTimeFormats))
            Console.WriteLine(dateOffsetValue.ToString("dddd", dateTimeFormats))
        Catch e As FormatException
            Console.WriteLine("Unable to convert {0} to a date.", dateString)
        End Try
    End Sub
End Module
' The example displays the following output to the console:
'       1
'       1
'       Mon
'       Mon
'       Monday
'       Monday
'       Mo
'       Mo
'       Mo
'       Mo
'       lun.
'       lun.
'       lundi
'       lundi

个别语言可能提供与 .NET 所提供的功能相同或互为补充的功能。 例如,Visual Basic 包括这样的两个函数:

  • Weekday,它返回指示特定日期中表示星期几的数字。 此函数将一周中第一天的序数值视为一,而 DateTime.DayOfWeek 属性却将其视为零。

  • WeekdayName,它返回当前区域性中与特定星期几相对应的周的名称。

下面的示例演示了 Visual Basic WeekdayWeekdayName 函数的用法:

Imports System.Globalization
Imports System.Threading

Module Example
    Public Sub Main()
        Dim dateValue As Date = #6/11/2008#

        ' Get weekday number using Visual Basic Weekday function
        Console.WriteLine(Weekday(dateValue))                 ' Displays 4
        ' Compare with .NET DateTime.DayOfWeek property
        Console.WriteLine(dateValue.DayOfWeek)                ' Displays 3

        ' Get weekday name using Weekday and WeekdayName functions
        Console.WriteLine(WeekdayName(Weekday(dateValue)))    ' Displays Wednesday

        ' Change culture to de-DE
        Dim originalCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
        Thread.CurrentThread.CurrentCulture = New CultureInfo("de-DE")
        ' Get weekday name using Weekday and WeekdayName functions
        Console.WriteLine(WeekdayName(Weekday(dateValue)))   ' Displays Donnerstag

        ' Restore original culture
        Thread.CurrentThread.CurrentCulture = originalCulture
    End Sub
End Module

也可以使用 DateTime.DayOfWeek 属性返回的值检索特定日期的星期几名称。 此过程只需对 ToString 属性返回的值调用 DayOfWeek 方法。 但是,此技术并不生成当前区域性的本地化星期几名称,如下面的示例所示:

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      // Change current culture to fr-FR
      CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

      DateTime dateValue = new DateTime(2008, 6, 11);
      // Display the DayOfWeek string representation
      Console.WriteLine(dateValue.DayOfWeek.ToString());
      // Restore original current culture
      Thread.CurrentThread.CurrentCulture = originalCulture;
   }
}
// The example displays the following output:
//       Wednesday
Imports System.Globalization
Imports System.Threading

Module Example
    Public Sub Main()
        ' Change current culture to fr-FR
        Dim originalCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
        Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")

        Dim dateValue As Date = #6/11/2008#
        ' Display the DayOfWeek string representation
        Console.WriteLine(dateValue.DayOfWeek.ToString())
        ' Restore original current culture
        Thread.CurrentThread.CurrentCulture = originalCulture
    End Sub
End Module
' The example displays the following output:
'       Wednesday

请参阅