Custom time format conversion code sharing

  • 2020-05-30 21:00:02
  • OfStack


/// <summary>
        ///  Converts a date string to a date type 
        /// </summary>
        /// <param name="strDateTime"> like "2012 years 5 month 14 day " Date string of </param>
        private DateTime ParseDateTime(string strDateTime)
        {
            ////Like: string strDateTime = "2012 years 5 month 14 day ";
            DateTime dateTime;
            if (DateTime.TryParseExact(strDateTime, "yyyy years M month d day ", new System.Globalization.CultureInfo("en-US"), System.Globalization.DateTimeStyles.AssumeLocal, out dateTime))
            {
                return dateTime;
            }
            else
            {
                return DateTime.Today;
            }
        }


Related articles: