C# 把字符串(String)格式转换为DateTime类型的三种方法

摘要:
方式一:Convert.ToDateTime(string)Convert.ToDateTime(string)注意:string格式有要求,必须是yyyy-MM-ddhh:mm:ss方式二:Convert.ToDateTime(string,IFormatProvider)1DateTimeFormatInfodtFormat=newSystem.GlobalizationDateTimeFor

方式一:Convert.ToDateTime(string)

Convert.ToDateTime(string)

注意:string格式有要求,必须是yyyy-MM-dd hh:mm:ss

方式二:Convert.ToDateTime(string, IFormatProvider)

1 DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();
2 dtFormat.ShortDatePattern = "yyyy/MM/dd";
3 DateTime  dt = Convert.ToDateTime("2016/10/10", dtFormat);

说明:任意格式可自定义规则。

方式三:DateTime.ParseExact()

string dateString = "20161010";
DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

说明:任意格式可自定义规则。

免责声明:文章转载自《C# 把字符串(String)格式转换为DateTime类型的三种方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇nodejs获取文件修改时间解决springboot——集成 mybatis遇到的问题:No MyBatis mapper was found in '[com.example.demo]' package...下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot+读取properties文件内容并注入到类属性中

第一种方法,以发送短信功能为例: 1.application.properties文件: sms.host=http://dingxin.market.alicloudapi.com sms.path=/dx/sendSms sms.method=POST sms.appcode=xxxxxxx 2.需要注入的类,在类的上面加上@Component,在类属...

C#文件处理相关内容

C#修改文件内容和过滤有效内容,文件格式一定。 OpenFileDialog file = new OpenFileDialog();//定义新的文件打开位置控件 file.InitialDirectory =Application.StartupPath; file.Filter...

解析数据库连接字符串 (将Data Source、Initial Catalog、User ID、Password取出)

private void AnalysisConnectionstring() { string tempStr = “Data Source=192.168.2.123;Initial Catalog=caxastat;Persist Security Info=True;User ID=sa;Passwor...

Android启动页面实现版本检查更新

1、引入模块 CheckUpdateLibrary的github地址如下: https://github.com/qiangxi/CheckUpdateLibrary 打包下载,复制该文件下的两个文件夹到项目的extras文件夹下, 项目setting.gradle里面加入这两个模块。 include ':app', ':extras:ok...

使用ganymed工具调用ssh2

需要引入ganymed-ssh2-build210.jar包。 其实很简单。所以直接贴代码,代码说话。 package com.eshore.framework.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream;...

JsonPath

JsonPath 在xml的使用过程中,对于xml的解析我们知道可以使用xpath的方式,随意的获取到我们想要的属性值。那么在使用json时,我们能不能实现同样的操作呢? 答案就是 json-path 基础介绍 跟 XPath 类似,JsonPath 通过路径来检索JSON,对语法格式如下 语法 符号 描述 $ 表示json的根节点,表示根节点下...