方式一:Convert.ToDateTime(string)

Convert.ToDateTime(string)

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

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

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

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

方式三:DateTime.ParseExact()

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

说明:任意格式可自定义规则,效果同方式二。

Net任意String格式转换为DateTime类型的更多相关文章

  1. C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)

    在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...

  2. 不使用java内置函数,将String字符串转换为int类型

    package com.test; public class AtoiTest { public static void main(String[] args) throws Exception { ...

  3. 将与系统时间格式不同的字符串格式化为DATETIME类型

    若系统时间格式为2012/03/05 08:12:12,那么若将("2012-03-05 08:12:12")格式化为时间变量时会报错,在转化之前先将系统时间格式改变再转换就不会报 ...

  4. C#支持从自定义日期时间格式到DateTime类型

            /// <summary>         ///         /// </summary>         /// <param name=&quo ...

  5. 将yyyyMMdd,dd/MM/yyyy 类型字符串转换为datetime 类型 yyyy-MM-dd C#

    DateTime ConvertDate = DateTime.ParseExact(", "yyyyMMdd", null, System.Globalization. ...

  6. c# 时间戳转换为Datetime类型的时间

    private static DateTime GetConvertTime(long ltime) { DateTime dt_time=new DateTime(1970,1,1,8,0,0,0) ...

  7. c# yyyyMMdd,dd/MM/yyyy 类型字符串转换为datetime 类型

    DateTime ConvertDate = DateTime.ParseExact("20140504", "yyyyMMdd", null, System. ...

  8. sql:String格式转换为时间进行比较

    字符串的格式为 yyyy-MM-dd HH:mm:ss str_to_date(a.time, '%Y-%m-%d %k:%i') < str_to_date(b.time, '%Y-%m-%d ...

  9. 字符串string类型转换成DateTime或DateTime?类型

    常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...

随机推荐

  1. python 装饰器初步学习

    第一步 简单函数 ''' 简单的函数:调用两次''' def myfunc(): print ('myfunc() called.') myfunc() myfunc() 第二步 装饰器为调用函数提供 ...

  2. sphinx的配置

    ## Sphinx configuration file sample## WARNING! While this sample file mentions all available options ...

  3. 从KRE到XRE:ASP.NET 5中正在消失的那些K

    前几天写了篇博客ASP.NET 5中的那些K,刚把ASP.NET 5中的那些K搞明白了些,昨天发现微软正在让那些K消失. 首先是在 KRuntime 的git日志中发现的: * Runtime ren ...

  4. [.NET领域驱动设计实战系列]专题六:DDD实践案例:网上书店订单功能的实现

    一.引言 上一专题已经为网上书店实现了购物车的功能了,在这一专题中,将继续对网上书店案例进行完善,本专题将对网上书店订单功能的实现进行介绍,现在废话不多说了,让我们来一起看看订单功能是如何实现的吧. ...

  5. 【腾讯bugly干货分享】Android自绘动画实现与优化实战——以Tencent OS录音机波形动

    前言 本文为腾讯bugly的原创内容,非经过本文作者同意禁止转载,原文地址为:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1180 ...

  6. Expression Tree 扩展MVC中的 HtmlHelper 和 UrlHelper

    表达式树是LINQ To everything 的基础,同时各种类库的Fluent API也 大量使用了Expression Tree.还记得我在不懂expression tree时,各种眼花缭乱的A ...

  7. IE浏览器不能自动显示PDF文件的解决办法

    今天更新了Adobe的PDF Reader,更新后发现在网页上无法预览PDF文件了,点击PDF的连接,浏览器就会提示下载或者打开,感觉很不爽,经过一番百度,找到了解决办法,在这里分享一下. 打开IE浏 ...

  8. 走进AngularJs(八) ng的路由机制

    在谈路由机制前有必要先提一下现在比较流行的单页面应用,就是所谓的single page APP.为了实现无刷新的视图切换,我们通常会用ajax请求从后台取数据,然后套上HTML模板渲染在页面上,然而a ...

  9. C struct结构体内存对齐问题

    在空间看到别人的疑问引起了我的兴趣,刚好是我感兴趣的话题,就写一下.为了别人的疑问,也发表在qq空间里.因为下班比较晚,10点才到家,发表的也晚.其实是个简单的问题.  直接用实例和内存图说明: #i ...

  10. css 常见时间轴的做法(————————————————时间轴——————————————————)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...