English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
يحتوي C# على بنية DateTime يمكنها معالجة التاريخ والوقت.
لإستخدام التاريخ والوقت في C#، استخدم كلمة المفتاح new لإنشاء عمودية للـ DateTime. أدناه يتم إنشاء عمودية DateTime بمعنى افتراضي.
DateTime dt = new DateTime(); // تعيين القيمة الافتراضية 01/01/0001 00:00:00
القيمة الافتراضية والقيمة الأدنى للـ DateTime هي 1 يناير 0001 00:00:00 (النصف الليل). يمكن أن تكون القيمة الأعلى هي 31 ديسمبر 9999 23:59:59
استخدام بناءات مختلفة لـ DateTime لتحديد القيم الافتراضية للـ DateTime.
// تعيين القيمة الافتراضية 01/01/0001 00:00:00 DateTime dt1 = new DateTime(); // تعيين السنة والشهر واليوم DateTime dt2 = new DateTime(2015, 12, 31); // تعيين السنة والشهر واليوم والساعة والدقيقة والثانية DateTime dt3 = new DateTime(2015, 12, 31, 5, 10, 20); // تعيين السنة والشهر واليوم والساعة والدقيقة والثانية والمنطقة الزمنية UTC DateTime dt4 = new DateTime(2015, 12, 31, 5, 10, 20, DateTimeKind.Utc);
في المثال السابق، قمنا بتحديد السنة والشهر واليوم في بناء المعادلة. يمكن أن تكون السنة بين 0001 و9999، والشهر بين 1 و12، واليوم بين 1 و31. ستؤدي أي قيمة أخرى تتجاوز هذه النطاقات إلى حدوث استثناء في الوقت الراهن.
DateTime dt = new DateTime(2015, 12, 32); // يسبب استثناء: اليوم خارج النطاق
استخدام مختلف بناءات DateTime لضبط التاريخ والوقت والمنطقة الزمنية والتقويم والمنطقة الثقافية.
التوقيت هو تاريخ ووقت تمثل بالعدد من النسق 100 نانو ثانية منذ 00:00:00.000 من 1 يناير 0001 من التقويم الجريغوري. أدناه يتم تحديد DateTime باستخدام عدد التوقيتات.
DateTime dt = new DateTime(636370000000000000); DateTime.MinValue.Ticks; // Minimum value DateTime.MaxValue.Ticks; // Maximum value
The DateTime structure includes static fields,properties and methods. The following examples demonstrate important static fields and properties.
DateTime currentDateTime = DateTime.Now; // Returns the current date and time DateTime todaysDate = DateTime.Today; // Returns today's date DateTime currentDateTimeUTC = DateTime.UtcNow; // Returns the current UTC date and time DateTime maxDateTimeValue = DateTime.MaxValue; // Returns the maximum value of DateTime DateTime minDateTimeValue = DateTime.MinValue; // Returns the minimum value of DateTime
TimeSpan is a structure used to represent time in days,hours,minutes,seconds and milliseconds.
DateTime dt = new DateTime(2015, 12, 31); TimeSpan ts = new TimeSpan(25, 20, 55); DateTime newDate = dt.Add(ts); Console.WriteLine(newDate); // 1/1/2016 1:20:55 AM
Subtracting two dates results in a TimeSpan.
DateTime dt1 = new DateTime(2015, 12, 31); DateTime dt2 = new DateTime(2016, 2, 2); TimeSpan result = dt2.Subtract(dt1); // 33.00:00:00
م reloading operators +،-،==!=،>،<،<=،>=،simulating the addition،subtraction and comparison of dates,which makes it easy to deal with dates.
DateTime dt1 = new DateTime(2015, 12, 20); DateTime dt2 = new DateTime(2016, 12, 31, 5, 10, 20); TimeSpan time = new TimeSpan(10, 5, 25, 50); Console.WriteLine(dt2 + time); // 1/10/2017 صباحاً 10:36:10 Console.WriteLine(dt2 - dt1); //377.05:10:20 Console.WriteLine(dt1 == dt2); //False Console.WriteLine(dt1 != dt2); //True Console.WriteLine(dt1 > dt2); //False Console.WriteLine(dt1 < dt2); //True Console.WriteLine(dt1 >= dt2); //False Console.WriteLine(dt1 <= dt2); //True
يحتوي هيكل DateTime على الطرق التالية لتحويل تاريخ和时间 إلى String.
الطريقة | وصف |
---|---|
ToString | تحويل قيمة DateTime إلى String بتنسيق معين في منطقة الحالي. |
ToShortDateString | تحويل قيمة DateTime إلى String قصير في منطقة الحالي (نمط M/d/yyyy). |
ToShortTimeString | تحويل قيمة DateTime إلى String قصير في منطقة الحالي (نمط h: mm: ss). |
ToLongDateString | تحويل قيمة DateTime إلى String طويل في منطقة الحالي (نمط dddd, MMMM d, yyyy). |
ToLongTimeString | تحويل قيمة DateTime إلى String طويل في منطقة الحالي (نمط h:mm:ss tt). |
في هذا المثال، يتم عرض كيفية تحويل DateTime إلى تنسيقات String المختلفة.
var dt = DateTime.Now; Console.WriteLine("تاريخ الحالي: " + dt.ToString("d")); Console.WriteLine("MM/dd/yyyy Format: " + dt.ToString("MM/dd/yyyy")) Console.WriteLine("dddd, dd MMMM yyyy Format: " + dt.ToString("dddd, dd MMMM yyyy")); Console.WriteLine("MM/dd/yyyy h:mm tt Format: " + dt.ToString("MM/dd/yyyy h:mm tt")); Console.WriteLine("MMMM dd Format: " + dt.ToString("MMMM dd")); Console.WriteLine("HH:mm:ss Format: " + dt.ToString("HH:mm:ss")); Console.WriteLine("hh:mm tt Format: " + dt.ToString("hh:mm tt")); Console.WriteLine("Short Date String: " + dt.ToShortDateString()); Console.WriteLine("Long Date String: " + dt.ToLongDateString()); Console.WriteLine("Short Time String: " + dt.ToShortTimeString()); Console.WriteLine("Long Time String: " + dt.ToLongTimeString());
يمكن استخدام الطرق Parse () و ParseExact () و TryParse () و TryParseExact () لتحويل النصوص المتمثلة للوقت والالتزامة إلى كائن DateTime.
إذا كان النص المحدد ليس تمثيلاً صالحاً للوقت والالتزامة، فإن طريقة Parse () و ParseExact () ستقوم بإطلاق استثناء. لذا، يُنصح باستخدام طريقة TryParse () أو TryParseExact ()، لأنها ستعود بـ false إذا كان النص غير صالح.
var str = "5/12/2020"; DateTime dt; var isValidDate = DateTime.TryParse(str, out dt); if(isValidDate) Console.WriteLine(dt); else Console.WriteLine($"{str} is not a valid date string");