新建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.日期格式参数含义说明

  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;

 获取当前日期是今年第几周

        /// <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类型的更多相关文章

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

    方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...

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

    (1 )Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss (2):Convert.ToDateTime(string, IFo ...

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

    原文链接:http://www.cnblogs.com/Pickuper/articles/2058880.html 方法一:Convert.ToDateTime(string) string格式有要 ...

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

    C#中并没有表示时间的变量,只有DateTime,所以要表示时间,可以用TimeSpan表示. 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-M ...

  5. C# 字符串转为DateTime类型

    方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...

  6. 【C#】string格式的日期转为DateTime类型及时间格式化处理方法

    日期格式:yyyyMMdd HH:mm:ss(注意此字符串的字母大小写很严格) yyyy:代表年份 MM: 代表月份 dd: 代表天 HH: 代表小时(24小时制) mm: 代表分钟 ss: 代表秒 ...

  7. 格式化日期时间字符串 Get-Date -Uformat , -format

    #将字符串格式化为时间格式 $dateTimeStr = '20141231T23:59:59' $format = 'yyyyMMddTHH:mm:ss' $formatProvider = [Gl ...

  8. MYSQL日期时间字符串互转

    --MYSQL date_format(date,'%Y-%m-%d') -------------->oracle中的to_char(); 日期时间转字符串 --MYSQL str_to_da ...

  9. 关于bat中日期时间字符串的格式化

    在其他编程语言中,要实现日期时间字符串的格式化,包括时间计算,都是比较简单的 但在bat或者说cmd.dos中要实现这些功能.还是有一定难度的 首先,windows的cmd中可以使用%date%表示日 ...

随机推荐

  1. spring+ibatis+多数据源

    环境:spring3.1+ibatis2.3.4+oracle+hbase要求:需要在工程中操作两个不同的数据源,一个是mssql,另一个是hbase.实现:    <bean id=" ...

  2. 2013 Visual Studio Magazine读者选择奖界面框架类获奖情况

    2013 Visual Studio Magazine读者选择奖已经正式揭晓了!据了解,截至今年此奖项已经评选了21次,非常值得.NET开发人员信赖和参考.此次评选共有400多个产品角逐28个分类的奖 ...

  3. Java学习心得之 Linux下搭建Java环境

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Java学习心得之 Linux下搭建Java环境 1.前言2.JDK安装3.配置环境变量4. ...

  4. Atitit.复合文档的格式 标准化格式

    Atitit.复合文档的格式 标准化格式 1. Docfile1 2. Iso   Cdf  cd file1 3. Zip1 4. Ooxml1 5. Odf  :OpenDocument Form ...

  5. 数据持久化之NSKeyedArchiver

    基本的数据类型如NSString.NSDictionary.NSArray.NSData.NSNumber等可以用属性列表的方法持久化到.plist 文件中,但如果是一些自定义的类的话,属性列表的方法 ...

  6. struts理解

    最近大家都在找工作,我好迷茫,觉得自己会的东西太少了.所以决定开始学习SSH三大框架. 首先是struts. struts是基于mvc模式的框架.(struts其实也是servlet封装,提高开发效率 ...

  7. Android实用代码七段(五)

      前言  每次分享意味着每次都有进步,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯 ...

  8. Eclipse中Maven+Spring3.2.17+SpringMVC HelloWorld

    遇到的问题 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path ...

  9. .Net魔法堂:提取注释生成API文档

    一.前言 在多人协作的项目中,除了良好的代码规范外,完整的API文档也相当重要.通过文档我们快速了解系统各模块的实际接口,及其使用场景.使用示例,一定程度上降低沟通成本,和减少后期维护中知识遗失等风险 ...

  10. Play Framework 完整实现一个APP(二)

    1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...