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();
}
}
}

C#-----类DateTime的常用方法的更多相关文章

  1. JAVA基础——Arrays工具类十大常用方法

    Arrays工具类十大常用方法 原文链接:http://blog.csdn.net/renfufei/article/details/16829457 0. 声明数组 String[] aArray ...

  2. JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较

    JAVA之旅(十六)--String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较 过节耽误了几天,我们继续JAVA之旅 一.String概述 String时 ...

  3. Java 线程类的一些常用方法

    线程类的一些常用方法: sleep(): 强迫一个线程睡眠N毫秒.  isAlive(): 判断一个线程是否存活.  join(): 等待线程终止.  activeCount(): 程序中活跃的线程数 ...

  4. python之time和datetime的常用方法

    python之time和datetime的常用方法   一.time的常用方法: import time,datetime # 时间有三种展现方式:时间戳,时间元组,格式化的时间print(time. ...

  5. C++实现日期转换类DateTime

    概述 工作中我们在网络传输时使用time_t来传输时间,在显示时使用字符串来显示,下面是一个日期转换类的实现,方便以后使用: // DateTime.hpp #ifndef _DATETIME_H # ...

  6. C# - 系统类 - DateTime类

    DateTime类 ns:System 此类是一个结构 提供了访问和修改它所代表的时间 创建DateTime实例的几种方式 DateTime time = , , , , , ); Console.W ...

  7. 时间通用类 datetime

    /// <summary> /// 时间通用类 /// </summary> public class DateTimeGeneral { /// <summary> ...

  8. C# String类&Math类&DateTime类

    String类: String a = "abcdefghijklmnopqrstuvwxyz"; int length = a.length;  //获取字符串的长度: a = ...

  9. 一个好的Java时间工具类DateTime

    此类的灵感来源于C# 虽然网上有什么date4j,但是jar太纠结了,先给出源码,可以继承到自己的util包中,作为一个资深程序员,我相信都有不少好的util工具类,我也希望经过此次分享,能带动技术大 ...

随机推荐

  1. shell grep

    grep "str" file > /dev/null if [ $? -eq 1]; then echo "no str" else echo &quo ...

  2. java常用的逻辑

    /** * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). * <p> * Licensed under th ...

  3. help文档制作 chm

    程序中的help文档制作 所用工具:HTML Help Workshop 文件包括:各个html文档,帮助页面的具体内容 hhc文档:help的目录文件 hhk文档:help的索引文件 MAP文件夹中 ...

  4. 5m21d缓冲区溢出学习笔记

    mysql链接字符串函数 concat(str1,str2) concat_ws(separator,str1,str2....) group_concat(str1,str2....) mysql的 ...

  5. html保留空格

    echo '<div style="white-space:pre-wrap">○ '.$noticeInfo[$i]['content'].'</div> ...

  6. spring boot异常积累

    1.异常:Error resolving template "xxx", template might not exist or might not be accessible.. ...

  7. MySQL8.0.15的安装与配置---win10

    1.下载地址 https://dev.mysql.com/downloads/installer/ 安装文件:mysql-installer-community-8.0.15.0.msi 2.安装 默 ...

  8. 报文分析6、ARP报头结构

    ARP报头结构   硬件类型 协议类型 硬件地址长度 协议长度 操作类型 发送方的硬件地址(0-3字节) 源物理地址(4-5字节) 源IP地址(0-1字节) 源IP地址(2-3字节) 目标硬件地址(0 ...

  9. 树莓派与Linux系统之间文件传输

    最近因为要学习Python,于是把放在家里接了一年灰的树莓派又给搜出来了,刚买那会也捣鼓了好一阵子, 基本操作都学会了,但现在又忘光了,只能又从头开始搞了,首先第一个要解决的是怎么把文件从电脑传输到树 ...

  10. thinkphp ckeditor与ckfinder

    thinkphp ckeditor与ckfinder 下载 ckeditor下载地址 ckfinder下载地址 整合 将ckeditor与findeditor下载完成后,放到public目录下,配置c ...