[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 = "20151126";
            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();//307
            System.Diagnostics.Debug.WriteLine(dt3.Hour.ToString());//dt3.Hour.ToString();//15
            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(1).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(1).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(1000).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(6000000000);//时间段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‘)[0].ToString());//2015-11-03T15:34:53
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘t‘)[0].ToString());//15:35
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘y‘)[0].ToString());//2015年11月
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘D‘)[0].ToString());//2015年11月3日
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘D‘)[1].ToString());//2015年11月3日,星期二
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘D‘)[2].ToString());//星期二,2015年11月3日
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘M‘)[0].ToString());//11月3日
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘f‘)[0].ToString());//2015年11月3日 15:40
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘g‘)[0].ToString());//2015/11/3 15:41
            System.Diagnostics.Debug.WriteLine(dt4.GetDateTimeFormats(‘r‘)[0].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(2015, 2);
            Console.WriteLine(days);
            //给日期增加一天、减少一天-----------------------------------------------
            DateTime dts = DateTime.Now;
            Console.WriteLine(dts.AddDays(1)); //增加一天
            Console.WriteLine(dts.AddDays(-1));//减少一天/*其它年份方法类似... */
            //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 = 0; i < int.MaxValue; i++)
            {
                // ...
            }
            long ticks1 = DateTime.Now.Ticks;
            long n = (ticks1 - ticks0) * 100;
            Console.WriteLine("上面这段程序运行了{0}毫微秒", n);

转换成秒比用毫微秒更直观些:

            long ticks0 = DateTime.Now.Ticks;
            for (int i = 0; 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.日期格式参数含义说明

  1. D 一周中的星期几
  2. DAY 天的名字,使用空格填充到9个字符
  3. DD 月中的第几天
  4. DDD 年中的第几天
  5. DY 天的简写名
  6. IW ISO标准的年中的第几周
  7. IYYY ISO标准的四位年份
  8. YYYY 四位年份
  9. YYY,YY,Y 年份的最后三位,两位,一位
  10. HH 小时,按12小时计
  11. HH24 小时,按24小时计
  12. MI 分
  13. SS 秒
  14. MM 月
  15. Mon 月份的简写
  16. Month 月份的全名
  17. W 该月的第几个星期
  18. 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;
时间: 2024-08-05 09:15:16

[No00003B]string格式的日期时间字符串转为DateTime类型的相关文章

【转】C#语言之“string格式的日期时间字符串转为DateTime类型”的方法

方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================================ 方法二:Convert.ToDateTime(string, IFormatProvider) DateTime dt; DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo()

C#语言之“string格式的日期时间字符串转为DateTime类型”的方法

方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================================ 方法二:Convert.ToDateTime(string, IFormatProvider) DateTime dt; DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo()

C# string格式的日期时间字符串转为DateTime类型

(1 )Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss (2):Convert.ToDateTime(string, IFormatProvider) DateTime dt; DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo(); dtFormat.ShortDatePattern = "yyyy/MM/dd";

C#中 String 格式的日期时间 转为 DateTime

C#中并没有表示时间的变量,只有DateTime,所以要表示时间,可以用TimeSpan表示. 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss 方法二:Convert.ToDateTime(string, IFormatProvider) DateTime dt; DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();

C# 字符串转为DateTime类型

方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================================ 方法二:Convert.ToDateTime(string, IFormatProvider) DateTime dt; DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo()

技术杂记-日期时间字符串解析识别

日期时间的格式可谓千奇百怪,做数据汇集相关项目时就会需要识别各种日期时间格式(因为数据来源广泛不可控),然后转换为标准格式或Date类型. 我之前看到同事的一个时间解析方法,当时觉得已经很不错了,后来网上搜索发现这个方法在搜索结果中多次出现,来源就不清楚.我是因项目需要更精确的识别日期时间字符串,于是我慢慢改进原来的方法,以下就是改进后的方法,java语言实现,只要看懂逻辑,应该很容易翻译成其他语言. 1 /** 2 * 解析大部分常见日期格式 <br/> 3 * @param dateStr

WPF XAML日期时间字符串的格式化

原文:WPF XAML日期时间字符串的格式化 XAML中的日期格式化跟程序代码中的格式化很类似,但有一些小小的不同: 对于某些控件我们可以使用ContentStringFormat 指定日期时间格式的方式: <Label Content="{Binding Path=BirthDate}" ContentStringFormat="{}{0:dd MM YYYY HH:mm:ss}" /> 使用默认日期格式显示日期的方式(显示的日期格式会随控制面板里的设

8. String to Integer (atoi) 字符串转为int类型的所有可能情况

8. String to Integer (atoi) 问题: 输入一个字符串,将字符串转为int类型,处理所有可能的输入情况. 可能的输入情况: 1.字符串为空.即"". 2.首先是假设输入的字符串都是数字型的,可含正负号,例如12345,+12548,-15568. 3.字符串中含有非数字的其他字符类型,例如a,-sd,-1286dgg558,000822fg55. 4.字符串首尾含有空格的,例如 -558dg12, 12dc12 . 5.针对转换后的数字越界,有两种情况,一个是超

将表某列时间信息转为datetime格式

import pandas as pd from dateutil.parser import parse #测试数据 test_dict = {0: {'startTime': 20190825131028, 'value': 1097}, 1: {'startTime': 20190825132458, 'value': 1083}, 2: {'startTime': 20190825143034, 'value': 1039}} df = pd.DataFrame(test_dict).T