时间标签DateTime
1:DateTime类型初始化:
DateTime date=new DateTime(2017,01,19,18,23,00);//此内部对应年,月,日,时,分,秒
Console.WriteLine(date);// 2017-01-19 18:23:00
2:获取当前系统时间
DateTime time=DateTime.now;//次默认类型是"yyyy-MM-dd HH:mm:ss"
3:对于时间字符串"yyyyMMddHHmmss"此状态转换成"yyyy-MM-dd HH:mm:ss"的解决办法
(1) 方法一
string str="20170119182300";
DateTime date=DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
Console.WriteLine(date);// 2017-01-19 18:23:00
【"System.Globalization.CultureInfo.CurrentCulture"这一属性我确实不知什么意思,若有大神看到帮忙解答】
其中"str"格式必须是"yyyyMMddHHmmss"时分秒可依次缺少,随之第二属性与第一属性时一一对应的,否则编译不通过。
(2)方法二
上面一种方法是直接返回被转环过来的时间DateTime,还有有种方法 DateTime.TryParse(str, out DateTime timeValue)
此方法返回Boolean类型,当可以转换并转换成功时返回true,当转换失败时返回false
转换成功之后虽然只有返回值true,但是转换后的DateTime时间同时赋给参数timeValue,只是不作为返回值返回,但是在后面
的操作中可以将timeValue作为已知变量;转换失败同转换成功,只是timeValue为System.DateTime.MinValue,
即0001-01-01 00:00:00。
例:
转换失败:
DateTime dateValue;
string str="201a-02-12";
bool b = DateTime.TryParse(str,out dateValue);
Console.WriteLine(b+","+dateValue);//false,0001-01-01 00:00:00
Console.ReadKey();
转换成功:
DateTime dateValue;
string str="2017-02-12";
bool b = DateTime.TryParse(str,out dateValue);
Console.WriteLine(b+","+dateValue);//True,2017-01-12 00:00:00
Console.ReadKey();
4:对于时间字符串"yyyy-MM-ddTHH:mm:ss"转换成字符串"yyyyMMddHHmmss"
string str = "2017-01-24T10:27:00";
DateTime date = DateTime.Parse(str).toString("yyyyMMddHHmmss");
5:对于默认类型DateTime"yyyy-MM-dd HH:mm:ss"转换成字符串"yyyyMMddHHmmss"
DateTime time=DateTime.now.ToString("yyyyMMddHHmmss")
6:自动化日期转成相应的DateTime时间yyyy-MM-dd HH:mm:ss
string number="40791";
DateTime date=DateTime FromOADate(Convert.ToDouble(number));
时间标签DateTime的更多相关文章
- ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效
ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效
- 带毫秒的字符转换成时间(DateTime)格式的通用方法
C#自身有更好的方式,Net任意String格式转换为DateTime类型 ====================================================== 原文 ==== ...
- Python学习总结15:时间模块datetime & time & calendar (二)
二 .datetime模块 1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...
- 2016年11月3日JS脚本简介数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6.布尔型数据:bool 7.对象类型:object 8.二进制:binary 语言类型: 1.强类型语言:c++ c c# java 2.弱类型语
数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6 ...
- winrar在右键菜单上加上:打包自动加上日期时间标签【图文教程】 - imsoft.cnblogs
说明: 注册表HKEY_CURRENT_USER\Software\WinRAR\Profiles\0找到GenerateArcName修改0为1,修改GenerateMask为你想要的日期式(默认 ...
- PHPCMS V9调用时间标签 |日期时间格式化
PHPCMS V9 如何调用时间标签,下面分享常见的调用时间标签 |日期时间格式化 1.日期时间格式化显示: a标准型:{date('Y-m-d H:i:s', $rs['inputtime'])} ...
- 【Python】随机模块random & 日期时间のtime&&datetime
■ random 顾名思义,random提供了python中关于模拟随机的一些方法.这些方法都一看就懂的,不多说了: random.random() 返回0<n<=1的随机实数 rando ...
- unix时间转换为datetime\datetime转换为unixtime
/// <summary> /// unix时间转换为datetime /// </summary> /// <param name="timeStamp&qu ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
随机推荐
- 关于MATLAB处理大数据坐标文件2017528
第一次提交数据 增加了部分特征 3000数据测试中得分99 但是10万数据出现过拟化现象,正确率下降 总结:1.某些特征数据本身波动不大应该考虑放弃 2.一些特征虽然表面觉得差异显而易见,但是数据表达 ...
- vue实现标签云效果
闲扯两句 最近想给自己的博客上加上一个3D标签云的效果,用来表示自己博客文章的分组,网上找到了canvas实现的,还有a元素实现的解析3D标签云,我想让标签可以选择和点击,又不想在标签数量较多时操作a ...
- 如何离线安装Visual Studio 2017
1. 官方下载在线安装文件 vs_community.exe https://www.visualstudio.com/zh-hans/thank-you-downloading-visual-stu ...
- PHP判断字符串中是否含有中文
<?php $str = "测试中文"; echo $str; echo "<hr>"; //if (preg_match("/^[ ...
- linux下如何解压和压缩文件
1.*.tar 用 tar –xvf 解压 2.*.gz 用 gzip -d或者gunzip 解压 3.*.tar.gz和*.tgz 用 tar –xzf 解压 4.*.bz2 用 bzip2 -d或 ...
- 【Android Developers Training】 35. 序言:分享文件
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 打开vmvare出现The VMware Authorization Service is not running。
win+r再输入cmd打开doc,输入services.msc打开服务,吧Vmware Authorization Service 更改为自动或者将其启动即可.
- 使用Cain&Abel进行网络嗅探
启动Cain&Abel并切换到"sniffer(嗅探)"标签. 第一步:需要对Cain&Abel进行配置,先单击最上面的"Configure(配置)&qu ...
- 教你如何取消GCD任务
GCD 是一种非常方便的使用多线程的方式.通过使用 GCD,我们可以在确保尽量简单的语法的前提下进行灵活的多线程编程.在 "复杂必死" 的多线程编程中,保持简单就是避免错误的金科玉 ...
- Spring事务管理的两种方式
参考文档: http://www.iteye.com/topic/1123347 http://blog.csdn.net/lcj8/article/details/2835432 PS:好像还是tx ...