Ejemplo n.º 1
0
        public static string ToInternalDate(string str, string indicator = "/")
        {
            try
            {
                if (str == null)
                {
                    return(null);
                }

                int      day        = 0;
                int      month      = 0;
                int      year       = 0;
                var      datestr    = "";
                string[] indicators = { indicator };
                var      datesplit  = str.Split(indicators, StringSplitOptions.RemoveEmptyEntries);
                if (datesplit.Length == 3)
                {
                    datestr = datesplit[0] + "/" + datesplit[1] + "/" + datesplit[2].Substring(0, 4);
                    day     = NumUtil.ParseInteger(datesplit[0]);
                    month   = NumUtil.ParseInteger(datesplit[1]);
                    year    = NumUtil.ParseInteger(datesplit[2].Substring(0, 4));
                }

                if (str.Contains(":"))
                {
                    int hour = 0;
                    int min  = 0;
                    int sec  = 0;

                    var newstr = str.Replace(datestr, "").Trim();
                    var dsplte = newstr.Split(':');

                    if (dsplte.Length == 2)
                    {
                        hour = NumUtil.ParseInteger(dsplte[0]);
                        min  = NumUtil.ParseInteger(dsplte[1]);
                        if (string.IsNullOrEmpty(dsplte[0]) && string.IsNullOrEmpty(dsplte[1]))
                        {
                            str = str.Replace(":", "").Trim();
                        }
                        //var d = DateTime.ParseExact(str, "dd" + indicator + "MM" + indicator + "yyyy HH:mm", null);
                        if (year < 1500)
                        {
                            year += 543;
                        }
                        else if (year > 2500)
                        {
                            year -= 543;
                        }

                        str = year.ToString("0000") + "-" + month.ToString("00") + "-" + day.ToString("00") + " " + hour.ToString("00") + ":" + min.ToString("00");
                        return(str);
                    }
                    else if (dsplte.Length == 3)
                    {
                        hour = NumUtil.ParseInteger(dsplte[0]);
                        min  = NumUtil.ParseInteger(dsplte[1]);
                        sec  = NumUtil.ParseInteger(dsplte[2]);
                        if (string.IsNullOrEmpty(dsplte[0]) && string.IsNullOrEmpty(dsplte[1]) && string.IsNullOrEmpty(dsplte[2]))
                        {
                            str = str.Replace(":", "").Trim();
                        }

                        if (year < 1500)
                        {
                            year += 543;
                        }
                        else if (year > 2500)
                        {
                            year -= 543;
                        }


                        str = year.ToString("0000") + "-" + month.ToString("00") + "-" + day.ToString("00") + " " + hour.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
                        return(str);
                    }
                }
                else
                {
                    if (year < 1500)
                    {
                        year += 543;
                    }
                    else if (year > 2500)
                    {
                        year -= 543;
                    }

                    str = year.ToString("0000") + "-" + month.ToString("00") + "-" + day.ToString("00");
                    return(str);
                }
            }
            catch
            {
                return("");
            }

            return("");
        }
Ejemplo n.º 2
0
        public static Nullable <DateTime> ToDate(string str, string indicator = "/", bool monthfirst = false)
        {
            try
            {
                if (str == null)
                {
                    return(null);
                }

                str = str.Replace("AM", "");
                str = str.Replace("PM", "");
                str = str.Trim();
                int day   = 0;
                int month = 0;
                int year  = 0;

                string[] indicators = { indicator };
                var      datesplit  = str.Split(indicators, StringSplitOptions.RemoveEmptyEntries);
                if (datesplit.Length == 3)
                {
                    if (monthfirst)
                    {
                        month = NumUtil.ParseInteger(datesplit[0]);
                        day   = NumUtil.ParseInteger(datesplit[1]);
                    }
                    else
                    {
                        day   = NumUtil.ParseInteger(datesplit[0]);
                        month = NumUtil.ParseInteger(datesplit[1]);
                    }
                    year = NumUtil.ParseInteger(datesplit[2].Substring(0, 4));
                }


                if (str.Contains(":"))
                {
                    int hour   = 0;
                    int min    = 0;
                    int sec    = 0;
                    var dsplte = str.Split(' ');
                    if (dsplte.Length >= 2)
                    {
                        dsplte = dsplte[1].Split(':');
                        if (dsplte.Length >= 2)
                        {
                            hour = NumUtil.ParseInteger(dsplte[0]);
                            min  = NumUtil.ParseInteger(dsplte[1]);
                            if (string.IsNullOrEmpty(dsplte[0]) && string.IsNullOrEmpty(dsplte[1]))
                            {
                                str = str.Replace(":", "").Trim();
                            }
                            //var d = DateTime.ParseExact(str, "dd" + indicator + "MM" + indicator + "yyyy HH:mm", null);
                            if (year < 1500)
                            {
                                year += 543;
                            }
                            else if (year > 2500)
                            {
                                year -= 543;
                            }

                            str = day.ToString("00") + indicator + month.ToString("00") + indicator + year.ToString() + " " + hour.ToString("00") + ":" + min.ToString("00");

                            return(DateTime.ParseExact(str, "dd" + indicator + "MM" + indicator + "yyyy HH:mm", new CultureInfo("en-US", false)));
                        }
                        else if (dsplte.Length == 3)
                        {
                            hour = NumUtil.ParseInteger(dsplte[0]);
                            min  = NumUtil.ParseInteger(dsplte[1]);
                            sec  = NumUtil.ParseInteger(dsplte[2]);
                            if (string.IsNullOrEmpty(dsplte[0]) && string.IsNullOrEmpty(dsplte[1]) && string.IsNullOrEmpty(dsplte[2]))
                            {
                                str = str.Replace(":", "").Trim();
                            }
                            //var d = DateTime.ParseExact(str, "dd" + indicator + "MM" + indicator + "yyyy HH:mm:ss", null);

                            if (year < 1500)
                            {
                                year += 543;
                                str   = day.ToString("00") + indicator + month.ToString("00") + indicator + year.ToString() + " " + hour.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
                            }
                            else if (year > 2500)
                            {
                                year -= 543;
                                str   = day.ToString("00") + indicator + month.ToString("00") + indicator + year.ToString() + " " + hour.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
                            }

                            return(DateTime.ParseExact(str, "dd" + indicator + "MM" + indicator + "yyyy HH:mm:ss", new CultureInfo("en-US", false)));
                        }
                    }
                }
                else
                {
                    str = day.ToString("00") + indicator + month.ToString("00") + indicator + year.ToString();
                    var d = DateTime.ParseExact(str, "dd" + indicator + "MM" + indicator + "yyyy", new CultureInfo("en-US", false));
                    if (d.Year < 1500)
                    {
                        year += 543;
                        str   = day.ToString("00") + indicator + month.ToString("00") + indicator + year.ToString();
                    }
                    else if (d.Year > 2500)
                    {
                        year -= 543;
                        str   = day.ToString("00") + indicator + month.ToString("00") + indicator + year.ToString();
                    }
                    return(DateTime.ParseExact(str, "dd" + indicator + "MM" + indicator + "yyyy", new CultureInfo("en-US", false)));
                }
            }
            catch (Exception ex)
            {
                return(null);
            }

            return(null);
        }