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. 阿里云负载均衡SSL证书配置(更新)

    阿里云负载均衡及应用防火墙SSL证书配置 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/8908461.html 好久了呢,距上篇博客的这段时间中:考试.搬家.工 ...

  2. [LeetCode] Bricks Falling When Hit 碰撞时砖头掉落

    We have a grid of 1s and 0s; the 1s in a cell represent bricks.  A brick will not drop if and only i ...

  3. ES6 模块机制

    ES6 实现了模块功能 将文件当作独立的模块,一个文件一个模块 每个模块可以导出自己的API成员,也可以导入其他模块或者模块中特定的API ES6 模块的设计思想,是尽量的静态化,使得编译时就能确定模 ...

  4. subline text3 安装 rem装换工具

    CSSREM 一个CSS的px值转rem值的Sublime Text 3自动完成插件. 插件效果如下: 安装 下载本项目,比如:git clone https://github.com/flashli ...

  5. 刀客139qq算命

    https://www.zhouyi.cc/bazi/sm/BaZi.php 好的算命网站 根据辛亥时看出来的午亥暗合没那么简单他不克你她太弱了婚姻不顺,是因为夫妻宫是财坏印是要比劫克财克财很重要 出 ...

  6. Linux下进程和端口常用操作

    https://blog.csdn.net/s573626822/article/details/80680456

  7. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

  8. sqlplus用户登录

    1.运行SQLPLUS工具 C:\Users\wd-pc>sqlplus 2.直接进入SQLPLUS命令提示符 C:\Users\wd-pc>sqlplus /nolog 3.以OS身份连 ...

  9. 【Bad Practice】12306 query

  10. 学习animate.css包含了一组炫酷、有趣、跨浏览器的动画

    1.animate.css包含了一组炫酷.有趣.跨浏览器的动画,可以在你的项目中直接使用. 第一步:引入animate.css样式文件或者引入某些平台的CDN文件: <head> < ...