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. CF 960 G

    难受的1b,怎么会这样 先去学写一发 NTT 大概说一下斯特林数

  2. 2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest

    A. Auxiliary Project 完全背包. #include<stdio.h> #include<iostream> #include<string.h> ...

  3. php替换字符串函数strtr()和str_repalce()区别

    php中替换函数主要有strtr(),str_repalce()这两个函数,但你们都知道他们这两个函数的区别和用法吗? 先来看看这个php字符串替换函数 strtr()的两种用法: strtr(str ...

  4. 1#Two Sum(qsort用法)

    void*空类型指针,就好像暂时还没有确定类型,任何类型都可以赋给它.但是具体操作时一定要确定类型(如下,比较时先转Node) cmp返回一定是int,有-1,0,1三种,如果是1则第一个数要放在第二 ...

  5. Bypass 360主机卫士SQL注入防御(附tamper脚本)

    0x01 前言 在测试过程中,经常会遇到一些主机防护软件,对这方面做了一些尝试,可成功bypass了GET和POST的注入防御,分享一下姿势. 0x02 环境搭建 Windows Server 200 ...

  6. github=>git=>composer Packages 使用教程

    2018年12月17日14:32:05 因为要做搜索,所以需要用分词工具php的分词不借助的第三方的真的很少, 目前选择的是 http://www.phpbone.com/phpanalysis/ 但 ...

  7. 颠覆传统的Word进阶

    第1课视频:无所不能的多样“替换”,为你换来大把时间 第2课视频:长文档的排版,又快又美又专业 - 之快 第3课视频:长文档的排版,又快又美又专业 - 之好 第4课视频:长文档的排版,又快又没有专业 ...

  8. golang 学习路径

    目录 一 了解 go 二 入门教程 三 安装运行环境 & IDE(goland) 四 gotour 五 简易源码解析 六 开始写代码 七 学习框架 八 惯用法 九 调优 一 了解 go 谷歌一 ...

  9. tomcat体系结构

    总体架构解析 Server: 一个StandardServer类实例就表示一个Server容器,TOMCAT启动的时候首先会启动一个Server,一个Server包括多个Service Service ...

  10. ZPW-2000电气绝缘移频轨道电路

    1.该轨道电路是个什么样子?(图片) 整个轨道电路不仅都在室外,其中还包括室内设备.其中室内设备包括发送器.接收器和衰耗盒.室内设备和室外设备通过一根电缆相连. 机柜 2.移频轨道电路有什么作用? z ...