Get the number of sub-parts for a certain time unit (e.g. 12 months for a given year). - CSharp System

CSharp examples for System:DateTime Year

Description

Get the number of sub-parts for a certain time unit (e.g. 12 months for a given year).

Demo Code

/* /*from   w  w w.j  a va2s.  c  o m*/
MIT License

Copyright (c) 2016-2017 Florian C?sar, Michael Plainer

For full license see LICENSE in the root directory of this project. 
*/
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
      /// Get the number of sub-parts for a certain time unit (e.g. 12 months for a given year).
      /// </summary>
      /// <param name="unit">The unit.</param>
      /// <returns>A number of sub-parts for a given time unit.</returns>
      public static int GetTimeUnitParts(this TimeUnit unit)
      {
         switch (unit)
         {
            case TimeUnit.Year:
               return 12;
            case TimeUnit.Month:
               return 4;
            case TimeUnit.Week:
               return 7;
            case TimeUnit.Day:
               return 24;
            case TimeUnit.Hour:
               return 60;
            case TimeUnit.Minute:
               return 60;
            case TimeUnit.Second:
               return 1000;
            case TimeUnit.Millisecond:
               return 1;
            default:
               throw new ArgumentException($"TimeUnit {unit} is not a valid time unit.");
         }
      }
}

Related Tutorials