To Short Date Time : DateTimeFormatInfo « Date Time « C# / C Sharp






To Short Date Time

      

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace NetworkAssetManager.WMI
{
    class WMIUtil
    {

        public static DateTime ToShortDateTime(string dmtfDate)
        {
            DateTime initializer = DateTime.MinValue;
            int year = initializer.Year;
            int month = initializer.Month;
            int day = initializer.Day;

            string dmtf = dmtfDate;
            DateTime datetime = DateTime.MinValue;
            string tempString = string.Empty;
            if ((dmtf == null))
            {
                return DateTime.MinValue;
            }
            if ((dmtf.Length == 0))
            {
                return DateTime.MinValue;
            }
            if ((dmtf.Length != 8))
            {
                return DateTime.MinValue;
            }
            try
            {
                tempString = dmtf.Substring(0, 4);
                if (("****" != tempString))
                {
                    year = int.Parse(tempString);
                }
                tempString = dmtf.Substring(4, 2);
                if (("**" != tempString))
                {
                    month = int.Parse(tempString);
                }
                tempString = dmtf.Substring(6, 2);
                if (("**" != tempString))
                {
                    day = int.Parse(tempString);
                }
                if (year < 0 || month < 0 ||  day < 0 )
                {
                    return DateTime.MinValue;
                }
            }
            catch (Exception)
            {
                return DateTime.MinValue;
            }
            datetime = new DateTime(year, month, day, 0, 0, 0, 0);

            return datetime;
        }
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.CultureInfo Provides information about a specific culture
2.Determines the specific cultures that use the Chinese language, and displays the parent culture
3.Gets or sets a DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.
4.Gets the list of calendars that can be used by the culture.
5.Custom Date and Time Format Strings
6.DateTimeFormatInfo Class
7.DateTimeFormatInfo.AbbreviatedMonthGenitiveNames
8.DateTimeFormatInfo.FullDateTimePattern
9.ALL the patterns
10.DateTimeFormatInfo.LongDatePattern
11.DateTimeFormatInfo.LongTimePattern
12.DateTimeFormatInfo.MonthDayPattern
13.DateTimeFormatInfo.RFC1123Pattern
14.DateTimeFormatInfo.ShortDatePattern
15.DateTimeFormatInfo.ShortTimePattern
16.DateTimeFormatInfo.SortableDateTimePattern
17.DateTimeFormatInfo.UniversalSortableDateTimePattern
18.DateTimeFormatInfo.YearMonthPattern
19.To Date Time from dmtf date