zoukankan      html  css  js  c++  java
  • C#-----类DateTime的常用方法

    1.TryParse(string s, out DateTime result)

       将日期和时间的指定字符串表示形式转换为其 System.DateTime 等效项,并返回一个指示转换是否成功的值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DateTimeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string timeStr = "2019-03-22 10:16:25";
                if (DateTime.TryParse(timeStr, out DateTime beginTime))
                {
                    Console.Write(beginTime);
                }
                else
                {
                    Console.WriteLine("时间格式不正确!");
                }
                Console.ReadLine();
            }
        }
    }

     2.Now

       获取一个 System.DateTime 对象,该对象设置为此计算机上的当前日期和时间,表示为本地时间

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DateTimeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime dateTime = DateTime.Now;
                Console.WriteLine(dateTime);
                Console.ReadLine();
            }
        }
    }

    3.ToString(string format)

       使用指定的格式和当前区域性的格式约定将当前 System.DateTime 对象的值转换为它的等效字符串表示形式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DateTimeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime dateTime = DateTime.Now;
                string nowTime = dateTime.ToString("yy-MM-dd hh:mm:ss");
                Console.WriteLine(nowTime);
                Console.ReadLine();
            }
        }
    }

     4.>(DateTime t1, DateTime t2)

         确定指定的 System.DateTime 是否晚于另一个指定的 System.DateTime

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DateTimeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string beginTime = "2019-04-22 21:25:15";
                string endTime = "2019-04-01 00:00:00";
                DateTime.TryParse(beginTime, out DateTime bTime);
                DateTime.TryParse(endTime, out DateTime eTime);
                if (bTime > eTime)
                {
                    Console.WriteLine("开始时间不能大于结束时间!");
                }
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    springboot 集成redission 以及分布式锁的使用
    springboot 使用线程池
    springboot 引入logback框架
    StringRedisTemplate 使用示例,工具类
    XHR post请求下载文件
    java List对象排序
    springboot中使用resttemplate调用服务是设置header中的accept-charset
    面试
    本地包打到项目中利用maven
    sql
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/10576707.html
Copyright © 2011-2022 走看看