• 时间格式相关操作


    1:获取,设置 系统时间:

    public class SetSystemDateTime//设置系统日期类
            {
                [DllImportAttribute("Kernel32.dll")]
                public static extern void GetLocalTime(SystemTime st);
                [DllImportAttribute("Kernel32.dll")]
                public static extern void SetLocalTime(SystemTime st);
            }
    
      [StructLayoutAttribute(LayoutKind.Sequential)]
            public class SystemTime//系统时间类
            {
                public ushort vYear;//
                public ushort vMonth;//
                public ushort vDayOfWeek;//星期
                public ushort vDay;//
                public ushort vHour;//小时
                public ushort vMinute;//
                public ushort vSecond;//
            }
    
    
    //实现
      DateTime Year = this.dateTimePicker1.Value;//得到时间信息
                    SystemTime MySystemTime = new SystemTime();//创建系统时间类的对象
                    SetSystemDateTime.GetLocalTime(MySystemTime);//得到系统时间
                    MySystemTime.vYear = (ushort)this.dateTimePicker1.Value.Year;//设置年
                    MySystemTime.vMonth = (ushort)this.dateTimePicker1.Value.Month;//设置月
                    MySystemTime.vDay = (ushort)this.dateTimePicker1.Value.Day;//设置日
                    MySystemTime.vHour = (ushort)this.dateTimePicker2.Value.Hour;//设置小时
                    MySystemTime.vMinute = (ushort)this.dateTimePicker2.Value.Minute;//设置分
                    MySystemTime.vSecond = (ushort)this.dateTimePicker2.Value.Second;//设置秒
                    SetSystemDateTime.SetLocalTime(MySystemTime);//设置系统时间

    2:根据生日计算年龄

    using Microsoft.VisualBasic;
    
          long P_BirthDay = DateAndTime.DateDiff(DateInterval.Year,//计算年龄
                     dtpicker_BirthDay.Value,DateTime.Now, 
                     FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1);

    3:根据年份判断12生肖

    System.Globalization.ChineseLunisolarCalendar chinseCaleander =//创建日历对象
              new System.Globalization.ChineseLunisolarCalendar();
     string TreeYear = "鼠牛虎兔龙蛇马羊猴鸡狗猪";//创建字符串对象
     int intYear = chinseCaleander.GetSexagenaryYear(DateTime.Now);//计算年信息
     string Tree = TreeYear.Substring(chinseCaleander.//得到生肖信息
                GetTerrestrialBranch(intYear) - 1, 1);
            //输出生肖信息 
     MessageBox.Show("今年是十二生肖" + Tree + "","判断十二生肖", MessageBoxButtons.OK,MessageBoxIcon.Information);

    4:判断当前日期是星期几

    MessageBox.Show("今天是: " + DateTime.Now.ToString("dddd"), "提示!");//显示星期信息

    5:判断当前年有多少天

    if (DateTime.IsLeapYear(int.Parse(DateTime.Now.ToString("yyyy"))))//判断是否为闰年
                {
                    MessageBox.Show("本年有366天","提示!");//显示天数信息
                }
                else
                {
                    MessageBox.Show("本年有365天", "提示!");//显示天数信息
                }

    6:计算当前月的天数

    int P_Count = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);//获取本月的天数
                MessageBox.Show("本月有" +P_Count.ToString() + "", "提示!");//显示本月的天数

    7:计算当前日期的前一天

    //显示前一天日期
                MessageBox.Show("昨天是:" + DateTime.Now.AddDays(-1).ToString("yyyy年M月d日"), "提示!");

    8:将日期转为指定格式

    lab_time.Text =
                    DateTime.Now.ToString("d") + "
    " +//使用指定格式的字符串变量格式化日期字符串
                    DateTime.Now.ToString("D") + "
    " +
                    DateTime.Now.ToString("f") + "
    " +
                    DateTime.Now.ToString("F") + "
    " +
                    DateTime.Now.ToString("g") + "
    " +
                    DateTime.Now.ToString("G") + "
    " +
                    DateTime.Now.ToString("R") + "
    " +
                    DateTime.Now.ToString("y") + "
    " +
                    "当前系统时间为:"+DateTime.Now.ToString(//使用自定义格式格式化字符串
                    "yyyy年MM月dd日 HH时mm分ss秒");

    结果显示:

    image

    9:用DateDiff获得日期之间的间隔数

    using Microsoft.VisualBasic;

    MessageBox.Show("间隔 "+DateAndTime.DateDiff(//使用DateDiff方法获取日期间隔
                   DateInterval.Day, dtpicker_first.Value, dtpicker_second.Value,
                   FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1).ToString()+" 天", "间隔时间");


    10:向当前时间添加一段时间

    using Microsoft.VisualBasic;
    
       private DateTime G_datetime;//定义时间字段
    
     G_datetime = DateAndTime.DateAdd( DateInterval.Minute, 1, G_datetime);//向时间字段中添加一分钟
     G_datetime = DateAndTime.DateAdd( DateInterval.Hour, 1, G_datetime);//向时间字段中添加一小时
     G_datetime = DateAndTime.DateAdd( DateInterval.Day, 1, G_datetime);//向时间字段中添加一天

    11:TimaSpan计算两个时间间隔

    private DateTime G_DateTime_First, G_DateTime_Second;//定义两个时间字段
    G_DateTime_First = DateTime.Now;//为时间字段赋值
     G_DateTime_Second = DateTime.Now;//为时间字段赋值
     TimeSpan P_timespan_temp =//计算两个时间的时间间隔
                    G_DateTime_First > G_DateTime_Second ?
                    G_DateTime_First - G_DateTime_Second :
                    G_DateTime_Second - G_DateTime_First;
    lab_result.Text = string.Format(//显示时间间隔
                    "间隔时间:{0}天{1}时{2}分{3}秒 {4}毫秒", 
                    P_timespan_temp.Days, P_timespan_temp.Hours,
                    P_timespan_temp.Minutes, P_timespan_temp.Seconds,
                    P_timespan_temp.Milliseconds);

    12:使用Sleep方法延时时间

    Thread th = new Thread(//创建线程对象
                    () =>//使用Lambda表达式
                    {
                        while (true)//无限循环
                        {
                            Invoke(//在窗体线程中执行
                                (MethodInvoker)(() =>//使用Lambda表达式
                                {
                                    txt_Time.Text =//显示系统时间
                                        DateTime.Now.ToString("F");
                                }));
                            Thread.Sleep(1000);//线程挂起1000毫秒
                        }
                    });
                th.IsBackground = true;//设置线程为后台线程
                th.Start();//开始执行线程

    13:计算程序的运行时间

    G_DateTime = DateTime.Now;//得到系统当前时间
                Thread P_th = new Thread(//创建线程
                    () =>//使用Lambda表达式
                    {
                        while (true)//无限循环
                        {
                            TimeSpan P_TimeSpan =//得到时间差
                                DateTime.Now - G_DateTime;
                            Invoke(//调用窗体线程
                                (MethodInvoker)(() =>//使用Lambda表达式
                                {
                                    tssLabel_Time.Text =//显示程序启动时间
                                        string.Format(
                                        "系统已经运行: {0}天{1}小时{2}分{3}秒",
                                        P_TimeSpan.Days, P_TimeSpan.Hours, 
                                        P_TimeSpan.Minutes, P_TimeSpan.Seconds);
                                }));
                            Thread.Sleep(1000);//线程挂起1秒钟
                        }
                    });
                P_th.IsBackground = true;//设置为后台线程
                P_th.Start();//开始执行线程

    14:用ParseExact将字符串转化为日期

    string s="2001/11/11";
    DateTime P_dt = DateTime.ParseExact(  s, "yyyy/MM/dd", null);//将字符串转换为日期格式

    15:使用ToString方法格式化日期

    lb_Format.Text += Environment.NewLine;
                lb_Format.Text += string.Format("{0}",DateTime.Now.ToString("F"));//使用指定日期格式化方式格式化字符串
                lb_Format.Text += Environment.NewLine;
                lb_Format.Text += string.Format("{0}", DateTime.Now.ToString("f"));//使用指定日期格式化方式格式化字符串
                lb_Format.Text += Environment.NewLine;
                lb_Format.Text += string.Format("{0}",DateTime.Now.ToString("D"));//使用指定日期格式化方式格式化字符串
                lb_Format.Text += Environment.NewLine;
                lb_Format.Text += string.Format("{0}",DateTime.Now.ToString("d"));//使用指定日期格式化方式格式化字符串
                lb_Format.Text += Environment.NewLine;
                lb_Format.Text += string.Format("{0}",DateTime.Now.ToString("G"));//使用指定日期格式化方式格式化字符串
                lb_Format.Text += Environment.NewLine;
                lb_Format.Text += string.Format("{0}",DateTime.Now.ToString("g"));//使用指定日期格式化方式格式化字符串
                lb_Format.Text += Environment.NewLine;
                lb_Format.Text += string.Format("{0}",DateTime.Now.ToString("yyyy-MM-dd hh:ss:ff"));//使用指定日期格式化方式格式化字符串

    运行结果:

    image

    16:使用ToDateTime转换日期显示格式

    DateTime P_dt = Convert.ToDateTime("2015/2/2");
                MessageBox.Show("输入的日期为: "+ P_dt.ToLongDateString(), "提示!");//弹出消息对话框
  • 相关阅读:
    搞懂:MVVM模型以及VUE中的数据绑定数据劫持发布订阅模式
    搞懂:前端跨域问题JS解决跨域问题VUE代理解决跨域问题原理
    搞懂js中小数运算精度问题原因及解决办法
    前端对base64编码的理解,原生js实现字符base64编码
    【置顶】我的联系方式
    浅析Block闭包
    再探NSString
    OC属性与实例变量
    IsEqual与Hash个人理解
    Copy与mutableCopy的个人理解
  • 原文地址:https://www.cnblogs.com/happyqiang/p/5495044.html
Copyright © 2020-2023  润新知