新建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. 对抽屉效果几大github第三方库的调研

    在公司项目新版本方案选择中,对主导航中要使用的抽屉效果进行了调研.主要原因是旧的项目中所用的库ECS评价不是很好.现对当下比较火的几大热门抽屉效果的第三方库进行了调研.代码全部选自github 如果你 ...

  2. Android环境搭建和编写helloworld

    一.配置jdk环境(学过java的请无视) 1.安装jdk jdk下载地址:http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk ...

  3. c中的函数

    一. 什么是函数 l 任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序段都有自己的功能,我们一般称这些程序段为“函数”.所以,你可以说C语言程序是由函数构成的. l 比如你用C语言 ...

  4. symfony2 controller

    1.基本概念 一次http请求    输入(Request):header信息.get信息.post数据等 输出(Response):symfony经过处理返回的信息,包括页面.json字符串.URL ...

  5. 面试题整理:C#(一)

    该系列持续更新,从网上以及身边收集的问题 1.可访问性级别有哪几种 public 访问不受限制.protected 访问仅限于包含类或从包含类派生的类型.internal 访问仅限于当前程序集.pro ...

  6. android ProGuard 代码混淆实现

    1 修改project.properties,添加ProGuard配置项 proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt: ...

  7. 编写可测试的JavaScript代码

    <编写可测试的JavaScript代码>基本信息作者: [美] Mark Ethan Trostler 托斯勒 著 译者: 徐涛出版社:人民邮电出版社ISBN:9787115373373上 ...

  8. .net开发windows服务小结

        今天学习了在.net下创建一个windows服务,总结一下学习心得.     开发环境:visual studio 2012   一.编写程序 (1)创建一个空解决方法 (2)添加一个控制台应 ...

  9. SQL Server 连接超时案例一则

    上周六,一工厂系统管理员反馈一数据库连接不上,SSMS连接数据库报"连接超时时间已到.在尝试使用预登录握手确认时超过了此超时时间.......", 如下截图所示: 另外远程连接也连 ...

  10. ORACLE数据库的限制

    ORACLE数据库最多可以拥有多少个表空间(Tablespace)?数据库最多拥有多少个数据文件(Database files).数据库的数据文件最大可以多大?遇到这些问题只能查询官方文档,人的记忆能 ...