Advertisement
fcamuso

Framework: date, parte seconda

May 19th, 2021
1,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DateTime_A
  4. {
  5.   class Program
  6.   {
  7.     static void Main(string[] args)
  8.     {
  9.       //TimeSpan, DateTime, DateTimeOffset
  10.      
  11.       DateTime dt1 = DateTime.Now;
  12.       DateTime dt2 = DateTime.Now;
  13.  
  14.       //Console.WriteLine(dt1);
  15.       DateTime dt3 = dt1 + TimeSpan.FromDays(5.5);
  16.       //Console.WriteLine(dt3);
  17.  
  18.       dt3 = dt3.AddDays(5);
  19.       //Console.WriteLine(dt3);
  20.       TimeSpan ts1 = new TimeSpan(10000000); //1s = 10.000.000 di ticks da 100ns
  21.       //Console.WriteLine(dt3 + ts1);
  22.       ts1 = dt3 - dt1 + TimeSpan.FromHours(11.999);
  23.  
  24.       //Console.WriteLine(ts1.TotalDays);
  25.       //Console.WriteLine(ts1.Days);
  26.  
  27.       //Console.WriteLine(DateTime.Now);
  28.       //Console.WriteLine(DateTimeOffset.Now);
  29.  
  30.       DateTimeOffset mezzogiornoLondra = new DateTimeOffset(2021, 5, 18, 12, 0, 0, TimeSpan.Zero);
  31.       DateTimeOffset quattordiciRoma = new DateTimeOffset(new DateTime(2021, 5, 18,14,0,0));
  32.  
  33.       Console.WriteLine(mezzogiornoLondra);
  34.       Console.WriteLine(quattordiciRoma);
  35.       Console.WriteLine(mezzogiornoLondra == quattordiciRoma);
  36.  
  37.       Console.WriteLine(DateTime.Today);
  38.  
  39.       string[] elencoDate = { "18/08/2018", "8/2018","07:22:16",
  40.                               "7 PM", "2018-08-18T07:22:16.0000000Z",
  41.                               "2018-08-18T07:22:16.0000000-07:00",
  42.                               "Sat, 18 Aug 2018 07:22:16 GMT"};
  43.       foreach (string d in elencoDate)
  44.       { dt3 = DateTime.Parse(d); Console.WriteLine(dt3.ToLongDateString()); }
  45.  
  46.       Console.WriteLine(dt3.DayOfWeek);
  47.       Console.WriteLine(dt3.DayOfYear);
  48.  
  49.       //i warning di Giovanni De Rosa
  50.  
  51.       //1. Neutralizzare/bypassare la parte time quando non serve
  52.       DateTime a = DateTime.Now; // Data di oggi
  53.       DateTime b = new DateTime(2021, 5, 19); // Data di oggi
  54.       if (a == b) Console.WriteLine("Date uguali"); // false
  55.       if (a.Date == b.Date) Console.WriteLine("Date uguali"); // false
  56.  
  57.       //2 costi mensili
  58.       //30 euro al mese = 360 euro anno.
  59.       //360 euro / 365 giorni = 0,9863
  60.       System.Globalization.GregorianCalendar calendar = new System.Globalization.GregorianCalendar();
  61.       DateTime ora = DateTime.Now;
  62.       decimal costoMensile = 30;
  63.       int anno = ora.Year;
  64.       int giorniAnno = calendar.GetDayOfYear(new DateTime(anno, 12, 31));
  65.       decimal costoGiornaliero = costoMensile * 12 / giorniAnno;
  66.       int mese = 2; // calcolo per febbraio
  67.       int giorniDelMese = DateTime.DaysInMonth(anno, mese);
  68.       decimal risultato = costoGiornaliero * giorniDelMese;
  69.       Console.WriteLine(risultato);
  70.  
  71.       //3. numero di giorni in un certo intervallo
  72.       dt1 = new DateTime(2021, 3, 1);
  73.       dt2 = new DateTime(2021, 3, 31);
  74.       Console.WriteLine($"Fatturati: {dt2 - dt1} giorni");
  75.    
  76.       //DateTimeOffset dtoff2 = new DateTimeOffset(dt3);
  77.       //Console.WriteLine(dtoff1);
  78.       //Console.WriteLine(dtoff2);
  79.  
  80.       //Console.WriteLine(ts1.Ticks);
  81.  
  82.  
  83.  
  84.     }
  85.   }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement