Parse Date Time - CSharp System

CSharp examples for System:DateTime Calculate

Description

Parse Date Time

Demo Code


using System.Globalization;
using System;/*w  w  w. j a  v a2s.c  om*/

public class Main{
        public static string ParseDatetime(DateTime? date, System.Globalization.CultureInfo culture)
        {
            if (date == null)
            {
                return string.Empty;
            }

            if (culture == null || culture.Name == "hr-HR")
            {
                var format = DateFormatCRO;
                return ((DateTime)date).ToString(format);
            }
            else
                throw new NotImplementedException("Not implemented for this culture");
        }
        public static DateTime? ParseDateTime(string textRepresentation, System.Globalization.CultureInfo culture)
        {
            if (string.IsNullOrEmpty(textRepresentation))
                return null;

            textRepresentation = textRepresentation.Trim();
            DateTime date;

            if (culture == null || culture.Name == "hr-HR")
            {
                var format = DateFormatCRO;

                if (!DateTime.TryParseExact(textRepresentation, format, new System.Globalization.CultureInfo("hr-HR"), System.Globalization.DateTimeStyles.None, out date))
                    throw new FormatException("Not supported format");
                else
                    return date;
            }
            else
                throw new NotImplementedException("Not implemented for this culture");
        }
}

Related Tutorials