[No00003B]string格式的日期时间字符串转为DateTime类型
新建console程序,复制粘贴直接运行:
/**/
//using System.Globalization;//代码测试大致时间2015/11/3 15:09:05
//方法一:Convert.ToDateTime(string)//string格式有要求,必须是yyyy - MM - dd hh:mm:ss
string sTime = "2015-11-3 14:25:25";
Console.WriteLine(Convert.ToDateTime(sTime));
//================================================
//方法二:Convert.ToDateTime(string, IFormatProvider)
DateTime dt;
System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
dtFormat.ShortDatePattern = "yyyy/MM/dd";
dt = Convert.ToDateTime("2015/11/2", dtFormat);
Console.WriteLine(dt);
//================================================
//方法三:DateTime.ParseExact()
string dateString = "";
DateTime dt1 = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);//或者
DateTime dt2 = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dt1);
Console.WriteLine(dt2);
//附参考信息:
CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
string format = "ddd MMM d HH:mm:ss zz00 yyyy";
string stringValue = DateTime.Now.ToString(format, cultureInfo); // 得到日期字符串
Console.WriteLine(stringValue);
DateTime datetime = DateTime.ParseExact("Wed Aug 25 16:28:03 +0800 2010", format, cultureInfo); // 将字符串转换成日期
Console.WriteLine(datetime);
//其他
//日期格式:yyyyMMdd HH:mm: ss [注意此字符串的字母大小写很严格]
//yyyy:代表年份
//MM: 代表月份
//dd: 代表天
//HH: 代表小时(24小时制)
//mm: 代表分钟
//ss: 代表秒
Console.WriteLine(DateTime.Now.ToShortTimeString());//DateTime.Now.ToShortTimeString()
DateTime dt3 = DateTime.Now;
Console.WriteLine(dt3.ToString());//2015/11/3 14:45:09
System.Diagnostics.Debug.WriteLine(dt3.ToFileTime().ToString());//dt3.ToFileTime().ToString();//130910068777372928 //输出对话框中右键,只显示程序输出
System.Diagnostics.Debug.WriteLine(dt3.ToFileTimeUtc().ToString());//dt3.ToFileTimeUtc().ToString();//和上面一样
System.Diagnostics.Debug.WriteLine(dt3.ToLocalTime().ToString());//dt3.ToLocalTime().ToString();//2015/11/3 14:52:22
System.Diagnostics.Debug.WriteLine(dt3.ToLongDateString().ToString());//dt3.ToLongDateString().ToString();//2015年11月3日
System.Diagnostics.Debug.WriteLine(dt3.ToLongTimeString().ToString());//dt3.ToLongTimeString().ToString();//14:59:50
System.Diagnostics.Debug.WriteLine(dt3.ToOADate().ToString());//dt3.ToOADate().ToString();//42311.6253648958 OLE自动化日期
System.Diagnostics.Debug.WriteLine(dt3.ToShortDateString().ToString());//dt3.ToShortDateString().ToString();//2015/11/3
System.Diagnostics.Debug.WriteLine(dt3.ToShortTimeString().ToString());//dt3.ToShortTimeString().ToString();//15:01
System.Diagnostics.Debug.WriteLine(dt3.ToUniversalTime().ToString());//dt3.ToUniversalTime().ToString();//2015/11/3 7:02:06 utc时间
System.Diagnostics.Debug.WriteLine(dt3.Year.ToString());//dt3.Year.ToString();//2015
System.Diagnostics.Debug.WriteLine(dt3.Date.ToString());//dt3.Date.ToString();//2015/11/3 0:00:00
System.Diagnostics.Debug.WriteLine(dt3.DayOfWeek.ToString());//dt3.DayOfWeek.ToString();//Tuesday
System.Diagnostics.Debug.WriteLine(dt3.DayOfYear.ToString());//dt3.DayOfYear.ToString();//
System.Diagnostics.Debug.WriteLine(dt3.Hour.ToString());//dt3.Hour.ToString();//
System.Diagnostics.Debug.WriteLine(dt3.Millisecond.ToString());//dt3.Millisecond.ToString();//503 实例所表示的毫秒部分
System.Diagnostics.Debug.WriteLine(dt3.Minute.ToString());//dt3.Minute.ToString();//5
System.Diagnostics.Debug.WriteLine(dt3.Month.ToString());//dt3.Month.ToString();//11
System.Diagnostics.Debug.WriteLine(dt3.Second.ToString());//dt3.Second.ToString();//28
System.Diagnostics.Debug.WriteLine(dt3.Ticks.ToString());//dt3.Ticks.ToString();//632667942284412864 635821599653192683 表示计时周期数
System.Diagnostics.Debug.WriteLine(dt3.TimeOfDay.ToString());//dt3.TimeOfDay.ToString();//15:08:42.0672822
System.Diagnostics.Debug.WriteLine(dt3.ToString());//dt3.ToString();//2015/11/3 15:09:05
System.Diagnostics.Debug.WriteLine(dt3.AddYears().ToString());//dt3.AddYears(1).ToString();//2016/11/3 15:09:17
System.Diagnostics.Debug.WriteLine(dt3.AddDays(1.1).ToString());//dt3.AddDays(1.1).ToString();//2015/11/4 17:34:05
System.Diagnostics.Debug.WriteLine(dt3.AddHours(1.1).ToString());//dt3.AddHours(1.1).ToString();//2015/11/3 16:16:20 增加1.1个小时
System.Diagnostics.Debug.WriteLine(dt3.AddMilliseconds(1.1).ToString());//dt3.AddMilliseconds(1.1).ToString();//2015/11/3 15:11:14
System.Diagnostics.Debug.WriteLine(dt3.AddMonths().ToString());//dt3.AddMonths(1).ToString();//2015/12/3 15:11:31
System.Diagnostics.Debug.WriteLine(dt3.AddSeconds(1.1).ToString());//dt3.AddSeconds(1.1).ToString();//2015/11/3 15:12:04
System.Diagnostics.Debug.WriteLine(dt3.AddMinutes(1.1).ToString());//dt3.AddMinutes(1.1).ToString();//2015/11/3 15:13:35
System.Diagnostics.Debug.WriteLine(dt3.AddTicks().ToString());//dt3.AddTicks(1000).ToString();//2015/11/3 15:12:59
System.Diagnostics.Debug.WriteLine(dt3.CompareTo(dt3).ToString());//dt3.CompareTo(dt3).ToString();//0
TimeSpan tTimeSpan = new TimeSpan();//时间段9个0 相当于10分钟
System.Diagnostics.Debug.WriteLine(tTimeSpan);
System.Diagnostics.Debug.WriteLine(dt3 - dt3.Add(tTimeSpan));
System.Diagnostics.Debug.WriteLine(dt3.Add(tTimeSpan).ToString());//dt3.Add(?).ToString();//问号为一个时间段
System.Diagnostics.Debug.WriteLine(dt3.Equals("2015-11-1 16:11:04").ToString());//dt3.Equals("2015-11-1 16:11:04").ToString();//False
System.Diagnostics.Debug.WriteLine(dt3.Equals(dt3).ToString());//dt3.Equals(dt).ToString();//True
System.Diagnostics.Debug.WriteLine(dt3.GetHashCode().ToString());//dt3.GetHashCode().ToString();//1474088234 2026765517
System.Diagnostics.Debug.WriteLine(dt3.GetType().ToString());//dt3.GetType().ToString();//System.DateTime
System.Diagnostics.Debug.WriteLine(dt3.GetTypeCode().ToString());//dt3.GetTypeCode().ToString();//DateTime DateTime dt4 = DateTime.Now;
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('s')[].ToString());//2015-11-03T15:34:53
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('t')[].ToString());//15:35
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('y')[].ToString());//2015年11月
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('D')[].ToString());//2015年11月3日
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('D')[].ToString());//2015年11月3日,星期二
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('D')[].ToString());//星期二,2015年11月3日
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('M')[].ToString());//11月3日
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('f')[].ToString());//2015年11月3日 15:40
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('g')[].ToString());//2015/11/3 15:41
System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats('r')[].ToString());//Tue, 03 Nov 2015 15:41:25 GMT
System.Diagnostics.Debug.WriteLine(string.Format("{0:d}", dt4));//2015/11/3
System.Diagnostics.Debug.WriteLine(string.Format("{0}", dt4));//2015/11/3 15:42:05
System.Diagnostics.Debug.WriteLine(string.Format("{0:f}", dt4));//2015年11月3日 15:42
System.Diagnostics.Debug.WriteLine(string.Format("{0:F}", dt4));//2015年11月3日 15:42:30
System.Diagnostics.Debug.WriteLine(string.Format("{0:g}", dt4));//2015/11/3 15:42
System.Diagnostics.Debug.WriteLine(string.Format("{0:G}", dt4));//2015/11/3 15:42:58
System.Diagnostics.Debug.WriteLine(string.Format("{0:M}", dt4));//11月3日
System.Diagnostics.Debug.WriteLine(string.Format("{0:R}", dt4));//Tue, 03 Nov 2015 15:43:22 GMT
System.Diagnostics.Debug.WriteLine(string.Format("{0:s}", dt4));//2015-11-03T15:43:41
System.Diagnostics.Debug.WriteLine(string.Format("{0:t}", dt4));//15:43
System.Diagnostics.Debug.WriteLine(string.Format("{0:T}", dt4));//15:44:12
System.Diagnostics.Debug.WriteLine(string.Format("{0:u}", dt4));//2015-11-03 15:44:22Z
System.Diagnostics.Debug.WriteLine(string.Format("{0:U}", dt4));//2015年11月3日 7:44:36
System.Diagnostics.Debug.WriteLine(string.Format("{0:Y}", dt4));//2015年11月
System.Diagnostics.Debug.WriteLine(string.Format("{0}", dt4));//2015/11/3 15:45:00
System.Diagnostics.Debug.WriteLine(string.Format("{0:yyyyMMddHHmmssffff}", dt4));//201511031545205768
//计算2个日期之间的天数差-----------------------------------------------
DateTime dtt1 = Convert.ToDateTime("2015-8-1");
DateTime dtt2 = Convert.ToDateTime("2015-8-15");
TimeSpan span = dtt1.Subtract(dtt2);
int dayDiff = span.Days;
Console.WriteLine(dayDiff);
//计算某年某月的天数-----------------------------------------------
int days = DateTime.DaysInMonth(, );
Console.WriteLine(days);
//给日期增加一天、减少一天-----------------------------------------------
DateTime dts = DateTime.Now;
Console.WriteLine(dts.AddDays()); //增加一天
Console.WriteLine(dts.AddDays(-));//减少一天/*其它年份方法类似... */
//Oracle SQL里转换日期函数-----------------------------------------------
//to_date("2015-6-6", 'YYYY-MM-DD");
//to_date("2015/6/6", 'yyyy/mm/dd");
Console.ReadKey();
1.DataTime.Now.Ticks精确的时间单位
getTime
public long getTime()
返回 自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
.net DateTime.Ticks
public long Ticks {get;}
属性值 :表示此实例的日期和时间的刻度数。该值介于 MinValue 和 MaxValue 之间。
此属性的值为自 0001 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。
一个返回的是毫秒一个返回的是微秒,所以知道毫秒与微妙之间的转化也是有必要的
1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)
1秒=1,000,000 微秒(μs) 1微秒=1/1,000,000秒(s)
1秒=1,000,000,000 纳秒(ns)
1纳秒=1/1,000,000,000秒(s)
1秒=1,000,000,000,000 皮秒(ps)
1 毫秒 = 10^-3 秒, ------->10的-3次方 小数点从1开始向左移3位即0.001
1 微秒 = 10^-6 秒,
1 毫微秒 = 10^-9 秒,
100 毫微秒 = 10^-7 秒。
Console.WriteLine(DateTime.Now.Ticks);//635821643608373430
也就是说,从0001 年 1 月 1 日午夜 12:00:00 以来到现在已经过了 635821643608373430* 10^-7 秒。这个很少用,除非需要很精确地知道从那时(1年1月1日)开始过了多少时间。
//常用于精确地计算两个时间差(想知道某段程序运行了多少毫微秒)。
long ticks0 = DateTime.Now.Ticks;
for (int i = ; i < int.MaxValue; i++)
{
// ...
}
long ticks1 = DateTime.Now.Ticks;
long n = (ticks1 - ticks0) * ;
Console.WriteLine("上面这段程序运行了{0}毫微秒", n);
转换成秒比用毫微秒更直观些:
long ticks0 = DateTime.Now.Ticks;
for (int i = ; i < int.MaxValue; i++)
{
// ...
}
long ticks1 = DateTime.Now.Ticks;
double n = (ticks1 - ticks0) / 10000000.0;
Console.WriteLine("上面这段程序运行了{0}秒", n);
2.to_date()与24小时制表示法及mm分钟的显示:
一、在使用Oracle的to_date函数来做日期转换时,很多Java程序员也许会直接的采用“yyyy-MM-dd HH:mm:ss”的格式作为格式进行转换,但是在Oracle中会引起错误:“ORA 01810 格式代码出现两次”。
select to_date('2015-01-01 13:14:20','yyyy-MM-dd HH24:mm:ss') from dual;
原因是SQL中不区分大小写,MM和mm被认为是相同的格式代码,所以Oracle的SQL采用了mi代替分钟。
select to_date('2015-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual;
二、另要以24小时的形式显示出来要用HH24
select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;//mi是分钟
select to_char(sysdate,'yyyy-MM-dd HH24:mm:ss') from dual;//mm会显示月份 oracle中的to_date参数含义
to_date参数含义
1.日期格式参数含义说明
- D 一周中的星期几
- DAY 天的名字,使用空格填充到9个字符
- DD 月中的第几天
- DDD 年中的第几天
- DY 天的简写名
- IW ISO标准的年中的第几周
- IYYY ISO标准的四位年份
- YYYY 四位年份
- YYY,YY,Y 年份的最后三位,两位,一位
- HH 小时,按12小时计
- HH24 小时,按24小时计
- MI 分
- SS 秒
- MM 月
- Mon 月份的简写
- Month 月份的全名
- W 该月的第几个星期
- WW 年中的第几个星期
1.日期时间间隔操作
select sysdate,sysdate - interval ’7’ MINUTE from dual;//当前时间减去7分钟的时间
select sysdate - interval ’7’ hour from dual;//当前时间减去7小时的时间
select sysdate - interval ’7’ day from dual;//当前时间减去7天的时间
select sysdate,sysdate - interval ’7’ month from dual;//当前时间减去7月的时间
select sysdate,sysdate - interval ’7’ year from dual;//当前时间减去7年的时间
select sysdate,sysdate - 8 *interval ’2’ hour from dual;//时间间隔乘以一个数字
2.日期到字符操作
select sysdate,to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’) from dual
select sysdate,to_char(sysdate,’yyyy-mm-dd hh:mi:ss’) from dual
select sysdate,to_char(sysdate,’yyyy-ddd hh:mi:ss’) from dual
select sysdate,to_char(sysdate,’yyyy-mm iw-d hh:mi:ss’) from dual
参考oracle的相关关文档(ORACLE901DOC/SERVER.901/A90125/SQL_ELEMENTS4.HTM#48515)
3. 字符到日期操作
select to_date(’2003-10-17 21:15:37’,’yyyy-mm-dd hh24:mi:ss’) from dual
具体用法和上面的to_char差不多。
4. trunk/ ROUND函数的使用
select trunc(sysdate ,’YEAR’) from dual
select trunc(sysdate ) from dual
select to_char(trunc(sysdate ,’YYYY’),’YYYY’) from dual
5.oracle有毫秒级的数据类型
select to_char(current_timestamp(5),’DD-MON-YYYY HH24:MI:SSxFF’) from dual; //返回当前时间 年月日小时分秒毫秒
select to_char(current_timestamp(9),’MI:SSxFF’) from dual;//返回当前 时间的秒毫秒,可以指定秒后面的精度(最大=9)
6.计算程序运行的时间(ms)
declare
type rc is ref cursor;
l_rc rc;
l_dummy all_objects.object_name%type;
l_start number default dbms_utility.get_time;
begin
for I in 1 .. 1000
loop
open l_rc for
’select object_name from all_objects ’||
’where object_id = ’ || i;
fetch l_rc into l_dummy;
close l_rc;
end loop;
dbms_output.put_line
( round( (dbms_utility.get_time-l_start)/100, 2 ) ||
’ seconds...’ );
end;
获取当前日期是今年第几周
/// <summary>
/// 取指定日期是一年中的第几周
/// </summary>
/// <param name="dt">给定的日期</param>
/// <returns>数字 一年中的第几周</returns>
private static int DatePart(DateTime dt)
{
int weeknow = Convert.ToInt32(dt.DayOfWeek);//今天星期几
int daydiff = (-) * (weeknow + );//今日与上周末的天数差
int days = System.DateTime.Now.AddDays(daydiff).DayOfYear;//上周末是本年第几天
int weeks = days / ;
if (days % != )
{
weeks++;
}
//此时,weeks为上周是本年的第几周
return (weeks + );
}
找的上面这个函数可以获取,但是第一周的处理不好,显示的是53周而不是1周
=================分割线=====================
后又根据本人最近的思路,写下了如下的程序,解决了上面第一周处理不好的地方,注释连着看就是我求解的思路算法
private static int GetWeekOfYear()
{
int firstWeekend = - Convert.ToInt32(DateTime.Parse(DateTime.Today.Year + "-1-1").DayOfWeek);//一.找到第一周的最后一天(先获取1月1日是星期几,从而得知第一周周末是几)
int currentDay = DateTime.Today.DayOfYear;//二.获取今天是一年当中的第几天
return Convert.ToInt32(Math.Ceiling((currentDay - firstWeekend) / 7.0)) + ;//三.(今天 减去 第一周周末)/7 等于 距第一周有多少周 再加上第一周的1 就是今天是今年的第几周了刚好考虑了惟一的特殊情况就是,今天刚好在第一周内,那么距第一周就是0 再加上第一周的1 最后还是1
}
[No00003B]string格式的日期时间字符串转为DateTime类型的更多相关文章
- 【转】C#语言之“string格式的日期时间字符串转为DateTime类型”的方法
方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...
- C# string格式的日期时间字符串转为DateTime类型
(1 )Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss (2):Convert.ToDateTime(string, IFo ...
- C#语言之“string格式的日期时间字符串转为DateTime类型”的方法(转)
原文链接:http://www.cnblogs.com/Pickuper/articles/2058880.html 方法一:Convert.ToDateTime(string) string格式有要 ...
- C#中 String 格式的日期时间 转为 DateTime
C#中并没有表示时间的变量,只有DateTime,所以要表示时间,可以用TimeSpan表示. 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-M ...
- C# 字符串转为DateTime类型
方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...
- 【C#】string格式的日期转为DateTime类型及时间格式化处理方法
日期格式:yyyyMMdd HH:mm:ss(注意此字符串的字母大小写很严格) yyyy:代表年份 MM: 代表月份 dd: 代表天 HH: 代表小时(24小时制) mm: 代表分钟 ss: 代表秒 ...
- 格式化日期时间字符串 Get-Date -Uformat , -format
#将字符串格式化为时间格式 $dateTimeStr = '20141231T23:59:59' $format = 'yyyyMMddTHH:mm:ss' $formatProvider = [Gl ...
- MYSQL日期时间字符串互转
--MYSQL date_format(date,'%Y-%m-%d') -------------->oracle中的to_char(); 日期时间转字符串 --MYSQL str_to_da ...
- 关于bat中日期时间字符串的格式化
在其他编程语言中,要实现日期时间字符串的格式化,包括时间计算,都是比较简单的 但在bat或者说cmd.dos中要实现这些功能.还是有一定难度的 首先,windows的cmd中可以使用%date%表示日 ...
随机推荐
- HTML基础知识总结
经过这段时间的学习,对于html的一些基础知识有了一定的了解.所谓好记性不如烂笔头,唯有一点点累积,才能汇聚成知识的海洋.现在,我对这段时间的学习做一个总结. 一.HTML的定义 HTML,超文本标记 ...
- PHP美元符和花括号组合那些事—${${}}
双美元符+{}:${${variable}}是一种比较常见的用法,但是它的实现原理是什么呢?今天来探究一下: 提及这种用法,还得先说一下PHP的String类型php.net上指出,一个字符串可以用4 ...
- iOS远程推送原理及实现过程
➠更多技术干货请戳:听云博客 推送通知,是现在的应用必不可少的功能.那么在 iOS 中,我们是如何实现远程推送的呢?iOS 的远程推送原理又是什么呢?在做 iOS 远程推送时,我们会遇到各种各样的问题 ...
- 打印frame
NSLog(@"%@",NSStringFromCGRect(switch.frame)); 或者 CFShow(NSStringFromCGRect(switch.frame)) ...
- Android中过场动画
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left); 第一参数为进入的动画 第二参数为退出的动画 进入的动画 ...
- 学习 jsonp
1.起因 js脚本做ajax异步调用的时候,直接请求普通文件存在跨域无权限访问的问题,不管你是静态页面.动态网页.web服务,只要是跨域请求,都无法成功: 如果上句话没明白,我们直接看例子.有两个一模 ...
- html的层叠次序---真没有想象的简单
学习资料1: http://www.cnblogs.com/mind/archive/2012/04/01/2198995.html 学习资料2:http://www.cnblogs.com/weib ...
- asp.net mvc 应用Bundle(捆绑和微小)压缩技术 启用 BundleConfig 配置web.config
从MVC4开始,我们就发现,项目中对Global.asax进行了优化,将原来在MVC3中使用的代码移到了 [App_Start]文件夹下,而Global.asax只负责初始化.其中的BundleCon ...
- cacti监控mysql
cacti监控mysql 2013-09-25 16:21:43 分类: LINUX 原文地址:cacti监控mysql 作者:baochenggood cacti监控mysql 1 下载cacti监 ...
- Hadoop+MongoDB的四种方案
背景: 公司核心业务库现存在MongoDB中,分布在6台MongoDB节点.现面临如下问题: 1.最大的一张表有10多个G,MongoDB在查询方面尚能胜任,但是涉及到复杂计算时会比较吃力. 2.Mo ...