欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

c# DataTime.toString()的显示精度问题,

来源: javaer 分享于  点击 26711 次 点评:151

c# DataTime.toString()的显示精度问题,


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ConsoleApp1

{

    class TimePorint

    {

        public string TimePorintTag;

        public string TimePorintValue;

        public string TimePorint2Key;

    }

    class Program

    {

        static void Main(string[] args)

        {

            DateTime TestTime = DateTime.Now;

            DateTime FormatNow = new DateTime();

            List<dynamic> TimePorintTags = new List<dynamic> { "Hour", "Day", "Week", "Month" };

            foreach (string TimePorintTag in TimePorintTags)//HOUR/DAY/WEEK/MONTH

            {

                try

                {

                    TimePorint tp = new TimePorint();

                    tp.TimePorintTag = TimePorintTag;

                    switch (TimePorintTag)

                    {

                        case "Hour":

                            tp.TimePorintValue = TestTime.ToString("yyyy-MM-dd HH:00:00");

                            tp.TimePorint2Key = TestTime.ToString("yyyyMMddHH");

                            break;

                        case "Day":

                            tp.TimePorintValue = TestTime.ToString("yyyy-MM-dd 00:00:00");

                            tp.TimePorint2Key = TestTime.ToString("yyyyMMdd00");

                            break;

                        case "Week":

                            tp.TimePorintValue = TestTime.AddDays(1 - DayOfWeek).ToString("yyyy-MM-dd 00:00:00");

                            tp.TimePorint2Key = TestTime.AddDays(1 - DayOfWeek).ToString("yyyyMMdd00");

                            break;

                        case "Month":

                            tp.TimePorintValue = TestTime.ToString("yyyy-MM-01 00:00:00");

                            tp.TimePorint2Key = TestTime.ToString("yyyyMM0100");

                            break;

                        default:

                            break;

                    }


                    //未指定部分正常输出,制定数字部分,按照数字输出,所以存在四舍五入

                    Console.WriteLine(tp.TimePorintTag);

                    Console.WriteLine(tp.TimePorintValue);

                    Console.WriteLine(tp.TimePorint2Key);


                    Console.WriteLine("just for fun!");

                    Console.WriteLine("tp is--"+tp+"--");//此处打印类名

                }

                catch (Exception ex)

                {

                    Console.WriteLine("Error:"+ex.ToString());

                }


                Console.ReadKey();

            }

        }

    }

}


相关文章

    暂无相关文章

用户点评