Return a string in the format yymmdd. For example, February 15, 2005 would be 050215. - CSharp System

CSharp examples for System:DateTime Format

Description

Return a string in the format yymmdd. For example, February 15, 2005 would be 050215.

Demo Code


using System.Text;
using System.Linq;
using System.IO;/*from ww  w.j  a  va 2 s  .  c om*/
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
      /// Return a string in the format yymmdd.
      /// For example, February 15, 2005 would be 050215.
      /// </summary>
      /// <param name="dateTime">The DateTime to extract the date. If none is passed in, uses today's date.</param>
      /// <returns>A string in the form yymmdd. For example, February 15, 2005 would be 050215.</returns>
      public static string getFileDateStyle(DateTime? dateTime = null)
      {
         string fileDateStyle = string.Empty;
      
         DateTime dt = (dateTime.HasValue ? dateTime.Value : DateTime.Now);
         int year = (dt.Year % 1000);
         int month = dt.Month;
         int day = dt.Day;

         fileDateStyle += year.ToString().PadLeft(2, '0');
         fileDateStyle += month.ToString().PadLeft(2, '0');
         fileDateStyle += day.ToString().PadLeft(2, '0');

         return fileDateStyle;
      }
}

Related Tutorials