操作說明:從特定日期擷取一星期的哪一日
.NET 可方便判斷特定日期是一週的第幾天,並可顯示特定日期當地語系化的工作日名稱。 您可以從 DayOfWeek 或 DayOfWeek 屬性取得指出對應於特定日期是星期幾的列舉值。 相對地,擷取工作日名稱是一種格式化作業,可藉由呼叫格式化方法來執行,例如日期和時間值的 ToString
方法或 String.Format 方法。 本文示範如何執行下列格式化作業。
擷取表示星期幾的數字
使用靜態 DateTime.Parse 或 DateTimeOffset.Parse 方法,將日期的字串表示轉換成 DateTime 或 DateTimeOffset 值。
使用 DateTime.DayOfWeek 或 DateTimeOffset.DayOfWeek 屬性,以擷取指出星期幾的 DayOfWeek 值。
若有需要,可將 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
擷取工作日名稱縮寫
使用靜態 DateTime.Parse 或 DateTimeOffset.Parse 方法,將日期的字串表示轉換成 DateTime 或 DateTimeOffset 值。
您可以擷取目前文化特性或特定文化特性的工作日名稱縮寫。
若要擷取目前文化特性的工作日名稱縮寫,請呼叫日期和時間值的 DateTime.ToString(String) 或 DateTimeOffset.ToString(String) 執行個體方法,並傳遞字串
ddd
做為format
參數。 下列範例說明如何呼叫 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
若要擷取特定文化特性的工作日名稱縮寫,請呼叫日期和時間值的 DateTime.ToString(String, IFormatProvider) 或 DateTimeOffset.ToString(String, IFormatProvider) 執行個體方法。 傳遞字串
ddd
做為format
參數。 傳遞 CultureInfo 或 DateTimeFormatInfo 物件,其代表您想要將其工作日名稱當作provider
參數來擷取的文化特性。 下列程式碼說明如何使用代表 fr-FR 文化特性的 CultureInfo 物件,來呼叫 ToString(String, IFormatProvider) 方法: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.
擷取完整的工作日名稱
使用靜態 DateTime.Parse 或 DateTimeOffset.Parse 方法,將日期的字串表示轉換成 DateTime 或 DateTimeOffset 值。
您可以擷取目前文化特性或特定文化特性的完整工作日名稱。
若要擷取目前文化特性的工作日名稱,請呼叫日期和時間值的 DateTime.ToString(String) 或 DateTimeOffset.ToString(String) 執行個體方法,並傳遞字串
dddd
做為format
參數。 下列範例說明如何呼叫 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
若要擷取特定文化特性的工作日名稱,請呼叫日期和時間值的 DateTime.ToString(String, IFormatProvider) 或 DateTimeOffset.ToString(String, IFormatProvider) 執行個體方法。 傳遞字串
dddd
做為format
參數。 傳遞 CultureInfo 或 DateTimeFormatInfo 物件,其代表您想要將其工作日名稱當作provider
參數來擷取的文化特性。 下列程式碼說明如何使用代表 es-ES 文化特性的 CultureInfo 物件,來呼叫 ToString(String, IFormatProvider) 方法: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.DayOfWeek 和 DateTimeOffset.DayOfWeek 屬性的呼叫,用於擷取代表特定日期是星期幾的數字。 也包含呼叫 DateTime.ToString 和 DateTimeOffset.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 Weekday
和 WeekdayName
函式:
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 屬性傳回的值來擷取特定日期的工作日名稱。 這個處理程序只需要在該屬性傳回的 DayOfWeek 值上呼叫 ToString 方法。 不過,此技巧並不會產生目前文化特性的當地語系化工作日名稱,如下列範例所說明:
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