Tuesday, October 3, 2017

DateTimeCOnversions

  class Program
    {
        static void Main(string[] args)
        {
            //string date = "01 April 2017";
            string[] dates = { "20170404", "20170703", "20170601", "20170405" };
            string dt;
            int lastday;

            string Month = "April", Year = "2017";
            string date = "01" + " " + Month + " " + Year;
            foreach (var item in dates)
            {
                //string date conversion
                dt = DateTime.ParseExact(item, "yyyyMMdd",
                CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
                Console.WriteLine("dts:" + dt);
            }

            //long date conversion
            date = (Convert.ToDateTime(date).ToString("yyyy-MM-dd"));
            lastday = DateTime.DaysInMonth(Convert.ToDateTime(date).Year, Convert.ToDateTime(date).Month);

            string lastdate = lastday.ToString() + " " + Month + " " + Year;
            lastdate = (Convert.ToDateTime(lastdate).ToString("yyyy-MM-dd"));

            Console.WriteLine("lastDay:" + lastday);
            Console.WriteLine("first date:" + date);
            Console.WriteLine("lastdate:" + (Convert.ToDateTime(lastdate).ToString("yyyy-MM-dd")));
            DateTime ldate = Convert.ToDateTime((Convert.ToDateTime(lastdate).ToString("yyyy-MM-dd")));
            DateTime fdate = Convert.ToDateTime(date);

            Console.WriteLine("fdate:" + fdate + ";ldate" + ldate);
            Console.ReadLine();
        }
    }

No comments:

Post a Comment