آموزش کار با تاریخ در #C

آموزش کار با تاریخ در #C

حالت‌های مختلف نمایش تاریخ و زمان با کمک String Formatting

static void Main(string[] args) { DateTime CurrTime = DateTime.Now; Console.WriteLine("DateTime display listing specifier and result:\n"); Console.WriteLine("d = {0:d}", CurrTime); // Short date mm/dd/yyyy Console.WriteLine("D = {0:D}", CurrTime); // Long date day, month dd, yyyy Console.WriteLine("f = {0:f}", CurrTime); // Full date/short time day, month dd, yyyy hh:mm Console.WriteLine("F = {0:F}", CurrTime); // Full date/full time day, month dd, yyyy HH:mm:ss AM/PM Console.WriteLine("g = {0:g}", CurrTime); // Short date/short time mm/dd/yyyy HH:mm Console.WriteLine("G = {0:G}", CurrTime); // Short date/long time mm/dd/yyyy hh:mm:ss Console.WriteLine("M = {0:M}", CurrTime); // Month dd Console.WriteLine("R = {0:R}", CurrTime); // ddd Month yyyy hh:mm:ss GMT Console.WriteLine("s = {0:s}", CurrTime); // yyyy-mm-dd hh:mm:ss can be sorted! Console.WriteLine("t = {0:t}", CurrTime); // Short time hh:mm AM/PM Console.WriteLine("T = {0:T}", CurrTime); // Long time hh:mm:ss AM/PM Console.WriteLine("u = {0:u}", CurrTime); // yyyy-mm-dd hh:mm:ss universal/sortable Console.WriteLine("U = {0:U}", CurrTime); // day, month dd, yyyy hh:mm:ss AM/PM Console.WriteLine("Y = {0:Y}", CurrTime); // Month, yyyy Console.WriteLine(); Console.WriteLine("DateTime.Month = " + CurrTime.Month); // number of month Console.WriteLine("DateTime.DayOfWeek = " + CurrTime.DayOfWeek); // full name of day Console.WriteLine("DateTime.TimeOfDay = " + CurrTime.TimeOfDay); Console.Read(); // wait }

برخی اعمال متدوال روی تاریخ ميلادی

// Create a TimeSpan representing 2.5 days. TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0); // Create a TimeSpan representing 4.5 days. TimeSpan timespan2 = new TimeSpan(4, 12, 0, 0); // Create a TimeSpan representing 1 week. TimeSpan oneWeek = timespan1 + timespan2; // Create a DateTime with the current date and time. DateTime now = DateTime.Now; // Create a DateTime representing 1 week ago. DateTime past = now - oneWeek; // Create a DateTime representing 1 week in the future. DateTime future = now + oneWeek;

مثال : پيدا کردن اختلاف تعداد روزهای بين دو تاریخ:

مثال 1:

DateTime dateFrom = DateTime.Parse("10/10/2007"); DateTime dateTo = DateTime.Parse("11/12/2007"); TimeSpan ts = dateTo - dateFrom; int days = ts.Days;

مثال 2:

DateTime dtFirst = new DateTime(2007, 10, 10); DateTime dtSecond = new DateTime(2007, 11, 12); TimeSpan diffResult = dtSecond.Subtract(dtFirst);

دستيابی به تاریخ شمسی با استفاده از PersianCalender

using System.Globalization; public string Miladi2Shamsi(DateTime _date) { PersianCalendar pc = new PersianCalendar(); StringBuilder sb = new StringBuilder(); sb.Append(pc.GetYear(_date).ToString("0000")); sb.Append("/"); sb.Append(pc.GetMonth(_date).ToString("00")); sb.Append("/"); sb.Append(pc.GetDayOfMonth(_date).ToString("00")); return sb.ToString(); }

نحوه ی استفاده، برای مثال تاریخ امروز:

string today = Miladi2Shamsi(DateTime.Now);

تبدیل رشته حاوی تاریخ شمسی به متناظر ميلادی آن

using System.Globalization; public DateTime Shamsi2Miladi(string _date) { int year = int.Parse(_date.Substring(0, 4)); int month = int.Parse(_date.Substring(5, 2)); int day = int.Parse(_date.Substring(8, 2)); PersianCalendar p = new PersianCalendar(); DateTime date = p.ToDateTime(year, month, day, 0, 0, 0, 0); return date; }



نظرات کاربران



عبارت امنیتی : حاصل 4 × 4 می شود :
متن پیام :