Aracılığıyla paylaş


Nasıl yapılır: Belirli bir Tarihten Haftanın Gününü Çıkarma

.NET, belirli bir tarih için haftanın sıra gününü belirlemeyi ve belirli bir tarih için yerelleştirilmiş haftanın günü adını görüntülemeyi kolaylaştırır. Veya DayOfWeek özelliğinden DayOfWeek belirli bir tarihe karşılık gelen haftanın gününü gösteren numaralandırılmış değer. Buna karşılık, haftanın günü adını almak, tarih ve saat değerinin ToString yöntemi veya yöntemi gibi bir biçimlendirme yöntemi çağrılarak gerçekleştirilen bir biçimlendirme işlemidir String.Format . Bu makalede, bu biçimlendirme işlemlerinin nasıl gerçekleştirebileceğiniz gösterilmektedir.

Haftanın gününü gösteren bir sayıyı ayıklama

  1. Bir tarihin dize gösterimini veya DateTimeOffset.Parse değerine dönüştürmek için DateTime statik DateTime.Parse veya DateTimeOffset yöntemini kullanın.

  2. DateTime.DayOfWeek Haftanın gününü gösteren bir DayOfWeek değer almak için or DateTimeOffset.DayOfWeek özelliğini kullanın.

  3. Gerekirse, değeri tamsayıya dönüştürün (C#'ta) veya dönüştürün DayOfWeek (Visual Basic'te).

Aşağıdaki örnekte, belirli bir tarihin haftanın gününü temsil eden bir tamsayı görüntülenir:

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

Kısaltılmış hafta içi adını ayıklama

  1. Bir tarihin dize gösterimini veya DateTimeOffset.Parse değerine dönüştürmek için DateTime statik DateTime.Parse veya DateTimeOffset yöntemini kullanın.

  2. Geçerli kültürün veya belirli bir kültürün kısaltılmış hafta içi adını ayıklayabilirsiniz:

    1. Geçerli kültürün kısaltılmış hafta içi adını ayıklamak için tarih ve saat değerinin DateTime.ToString(String) veya DateTimeOffset.ToString(String) örnek yöntemini çağırın ve dizeyi ddd parametre olarak format geçirin. Aşağıdaki örnekte yöntemine yapılan çağrı gösterilmektedir 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. Belirli bir kültürün kısaltılmış hafta içi adını ayıklamak için tarih ve saat değerinin DateTime.ToString(String, IFormatProvider) veya DateTimeOffset.ToString(String, IFormatProvider) örnek yöntemini çağırın. Dizeyi ddd parametre olarak format geçirin. CultureInfo Parametre olarak provider hafta içi adını almak istediğiniz kültürü temsil eden bir DateTimeFormatInfo veya nesnesini geçirin. Aşağıdaki kod, fr-FR kültürünü temsil eden bir CultureInfo nesne kullanarak yöntemine yapılan çağrıyı ToString(String, IFormatProvider) gösterir:

      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.
      

Tam haftanın günü adını ayıklama

  1. Bir tarihin dize gösterimini veya DateTimeOffset.Parse değerine dönüştürmek için DateTime statik DateTime.Parse veya DateTimeOffset yöntemini kullanın.

  2. Geçerli kültürün veya belirli bir kültürün tam haftanın günü adını ayıklayabilirsiniz:

    1. Geçerli kültürün hafta içi adını ayıklamak için tarih ve saat değerinin DateTime.ToString(String) veya DateTimeOffset.ToString(String) örnek yöntemini çağırın ve dizeyi dddd parametre olarak format geçirin. Aşağıdaki örnekte yöntemine yapılan çağrı gösterilmektedir 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. Belirli bir kültürün hafta içi adını ayıklamak için tarih ve saat değerinin DateTime.ToString(String, IFormatProvider) veya DateTimeOffset.ToString(String, IFormatProvider) örnek yöntemini çağırın. Dizeyi dddd parametre olarak format geçirin. CultureInfo Parametre olarak provider hafta içi adını almak istediğiniz kültürü temsil eden bir DateTimeFormatInfo veya nesnesini geçirin. Aşağıdaki kod, es-ES kültürünü temsil eden bir CultureInfo nesne kullanarak yöntemine yapılan çağrıyı ToString(String, IFormatProvider) gösterir:

      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.
      

Örnek

Aşağıdaki örnekte, belirli bir tarih için DateTime.DayOfWeek haftanın gününü temsil eden numarayı almak için ve DateTimeOffset.DayOfWeek özelliklerine yapılan çağrılar gösterilmektedir. Ayrıca kısaltılmış haftanın günü adını ve tam hafta içi adını ayıklamak için ve DateTimeOffset.ToString yöntemlerine çağrılar DateTime.ToString içerir.

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

Tek tek diller, .NET tarafından sağlanan işlevselliği yineleyen veya destekleyen işlevler sağlayabilir. Örneğin, Visual Basic bu tür iki işlev içerir:

  • Weekday, belirli bir tarihin haftanın gününü gösteren bir sayı döndürür. Haftanın ilk gününün sıra değerini bir olarak değerlendirirken DateTime.DayOfWeek özelliği bunu sıfır olarak kabul eder.

  • WeekdayName, geçerli kültürde belirli bir haftanın günü numarasına karşılık gelen haftanın adını döndürür.

Aşağıdaki örnekte Visual Basic Weekday ve WeekdayName işlevlerinin kullanımı gösterilmektedir:

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

Belirli bir tarihin haftanın günü adını almak için özelliği tarafından DateTime.DayOfWeek döndürülen değeri de kullanabilirsiniz. Bu işlem yalnızca özelliği tarafından döndürülen değerde DayOfWeek yöntemine bir çağrı ToString gerektirir. Ancak bu teknik, aşağıdaki örnekte gösterildiği gibi geçerli kültür için yerelleştirilmiş bir hafta içi adı oluşturmaz:

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

Ayrıca bkz.