zoukankan      html  css  js  c++  java
  • .NET基础 (12)时间的操作System.DateTime

    时间的操作System.DateTime
    1 DateTime如何存储时间
    2 如何在DateTime对象和字符串对象之间进行转换
    3 什么是UTC时间,如何转换到UTC时间

    时间的操作System.DateTime
    1 DateTime如何存储时间

    DateTime结果内部存储在一个64位长的整数,其中低62位来代表时间,它表示从0001年1月1日0时0分0秒开始到表示的时间经过百毫微秒数


    2 如何在DateTime对象和字符串对象之间进行转换

    DateTime可以通过ToString方法来格式化输出到字符串。

    TryParse和Parse方法则用来从一个字符串中得到信息并构建一个DateTime对象。


    3 什么是UTC时间,如何转换到UTC时间

    UTC是格林威治标准时间(GMT),它是在0度经度测量到的时间。通过ToUniversalTime和ToLocal方法可以在本地时间和UTC时间之间进行转换。两个算法在转换时会相应地设置保存时间的64位整数的高两位标志,并且转换的算法考虑了夏令时。

    对时间的一些操作:

    //当前时间now:2015/9/10 16:05:43
    now.GetDateTimeFormats('d')[0]  //2015/9/10
    now.GetDateTimeFormats('d')[1]  //2015-9-10
    now.GetDateTimeFormats('d')[2]  //2015.9.10
    now.GetDateTimeFormats('d')[3]  //2015-09-10
    now.GetDateTimeFormats('d')[4]  //2015/09/10
    now.GetDateTimeFormats('d')[5]  //2015.09.10
    now.GetDateTimeFormats('d')[6]  //15-9-10
    now.GetDateTimeFormats('d')[7]  //15/9/10
    now.GetDateTimeFormats('d')[8]  //15.9.10
    now.GetDateTimeFormats('d')[9]  //15/09/10
    now.GetDateTimeFormats('D')[0]  //2015年9月10日
    now.GetDateTimeFormats('D')[1]  //2015年9月10日,星期四
    now.GetDateTimeFormats('D')[2]  //星期四,2015年9月10日
    now.GetDateTimeFormats('f')[0]  //2015年9月10日 16:05
    now.GetDateTimeFormats('f')[1]  //2015年9月10日 16:05
    now.GetDateTimeFormats('f')[2]  //2015年9月10日 下午 4:05
    now.GetDateTimeFormats('f')[3]  //2015年9月10日 下午 04:05
    now.GetDateTimeFormats('f')[4]  //2015年9月10日,星期四 16:05
    now.GetDateTimeFormats('f')[5]  //2015年9月10日,星期四 16:05
    now.GetDateTimeFormats('f')[6]  //2015年9月10日,星期四 下午 4:05
    now.GetDateTimeFormats('f')[7]  //2015年9月10日,星期四 下午 04:05
    now.GetDateTimeFormats('f')[8]  //星期四,2015年9月10日 16:05
    now.GetDateTimeFormats('f')[9]  //星期四,2015年9月10日 16:05
    now.GetDateTimeFormats('f')[10]  //星期四,2015年9月10日 下午 4:05
    now.GetDateTimeFormats('f')[11]  //星期四,2015年9月10日 下午 04:05
    now.GetDateTimeFormats('F')[0]  //2015年9月10日 16:05:43
    now.GetDateTimeFormats('F')[1]  //2015年9月10日 16:05:43
    now.GetDateTimeFormats('F')[2]  //2015年9月10日 下午 4:05:43
    now.GetDateTimeFormats('F')[3]  //2015年9月10日 下午 04:05:43
    now.GetDateTimeFormats('F')[4]  //2015年9月10日,星期四 16:05:43
    now.GetDateTimeFormats('F')[5]  //2015年9月10日,星期四 16:05:43
    now.GetDateTimeFormats('F')[6]  //2015年9月10日,星期四 下午 4:05:43
    now.GetDateTimeFormats('F')[7]  //2015年9月10日,星期四 下午 04:05:43
    now.GetDateTimeFormats('F')[8]  //星期四,2015年9月10日 16:05:43
    now.GetDateTimeFormats('F')[9]  //星期四,2015年9月10日 16:05:43
    now.GetDateTimeFormats('F')[10]  //星期四,2015年9月10日 下午 4:05:43
    now.GetDateTimeFormats('F')[11]  //星期四,2015年9月10日 下午 04:05:43
    now.GetDateTimeFormats('g')[0]  //2015/9/10 16:05
    now.GetDateTimeFormats('g')[1]  //2015/9/10 16:05
    now.GetDateTimeFormats('g')[2]  //2015/9/10 下午 4:05
    now.GetDateTimeFormats('g')[3]  //2015/9/10 下午 04:05
    now.GetDateTimeFormats('g')[4]  //2015-9-10 16:05
    now.GetDateTimeFormats('g')[5]  //2015-9-10 16:05
    now.GetDateTimeFormats('g')[6]  //2015-9-10 下午 4:05
    now.GetDateTimeFormats('g')[7]  //2015-9-10 下午 04:05
    now.GetDateTimeFormats('g')[8]  //2015.9.10 16:05
    now.GetDateTimeFormats('g')[9]  //2015.9.10 16:05
    now.GetDateTimeFormats('g')[10]  //2015.9.10 下午 4:05
    now.GetDateTimeFormats('g')[11]  //2015.9.10 下午 04:05
    now.GetDateTimeFormats('g')[12]  //2015-09-10 16:05
    now.GetDateTimeFormats('g')[13]  //2015-09-10 16:05
    now.GetDateTimeFormats('g')[14]  //2015-09-10 下午 4:05
    now.GetDateTimeFormats('g')[15]  //2015-09-10 下午 04:05
    now.GetDateTimeFormats('g')[16]  //2015/09/10 16:05
    now.GetDateTimeFormats('g')[17]  //2015/09/10 16:05
    now.GetDateTimeFormats('g')[18]  //2015/09/10 下午 4:05
    now.GetDateTimeFormats('g')[19]  //2015/09/10 下午 04:05
    now.GetDateTimeFormats('g')[20]  //2015.09.10 16:05
    now.GetDateTimeFormats('g')[21]  //2015.09.10 16:05
    now.GetDateTimeFormats('g')[22]  //2015.09.10 下午 4:05
    now.GetDateTimeFormats('g')[23]  //2015.09.10 下午 04:05
    now.GetDateTimeFormats('g')[24]  //15-9-10 16:05
    now.GetDateTimeFormats('g')[25]  //15-9-10 16:05
    now.GetDateTimeFormats('g')[26]  //15-9-10 下午 4:05
    now.GetDateTimeFormats('g')[27]  //15-9-10 下午 04:05
    now.GetDateTimeFormats('g')[28]  //15/9/10 16:05
    now.GetDateTimeFormats('g')[29]  //15/9/10 16:05
    now.GetDateTimeFormats('g')[30]  //15/9/10 下午 4:05
    now.GetDateTimeFormats('g')[31]  //15/9/10 下午 04:05
    now.GetDateTimeFormats('g')[32]  //15.9.10 16:05
    now.GetDateTimeFormats('g')[33]  //15.9.10 16:05
    now.GetDateTimeFormats('g')[34]  //15.9.10 下午 4:05
    now.GetDateTimeFormats('g')[35]  //15.9.10 下午 04:05
    now.GetDateTimeFormats('g')[36]  //15/09/10 16:05
    now.GetDateTimeFormats('g')[37]  //15/09/10 16:05
    now.GetDateTimeFormats('g')[38]  //15/09/10 下午 4:05
    now.GetDateTimeFormats('g')[39]  //15/09/10 下午 04:05
    now.GetDateTimeFormats('G')[0]  //2015/9/10 16:05:43
    now.GetDateTimeFormats('G')[1]  //2015/9/10 16:05:43
    now.GetDateTimeFormats('G')[2]  //2015/9/10 下午 4:05:43
    now.GetDateTimeFormats('G')[3]  //2015/9/10 下午 04:05:43
    now.GetDateTimeFormats('G')[4]  //2015-9-10 16:05:43
    now.GetDateTimeFormats('G')[5]  //2015-9-10 16:05:43
    now.GetDateTimeFormats('G')[6]  //2015-9-10 下午 4:05:43
    now.GetDateTimeFormats('G')[7]  //2015-9-10 下午 04:05:43
    now.GetDateTimeFormats('G')[8]  //2015.9.10 16:05:43
    now.GetDateTimeFormats('G')[9]  //2015.9.10 16:05:43
    now.GetDateTimeFormats('G')[10]  //2015.9.10 下午 4:05:43
    now.GetDateTimeFormats('G')[11]  //2015.9.10 下午 04:05:43
    now.GetDateTimeFormats('G')[12]  //2015-09-10 16:05:43
    now.GetDateTimeFormats('G')[13]  //2015-09-10 16:05:43
    now.GetDateTimeFormats('G')[14]  //2015-09-10 下午 4:05:43
    now.GetDateTimeFormats('G')[15]  //2015-09-10 下午 04:05:43
    now.GetDateTimeFormats('G')[16]  //2015/09/10 16:05:43
    now.GetDateTimeFormats('G')[17]  //2015/09/10 16:05:43
    now.GetDateTimeFormats('G')[18]  //2015/09/10 下午 4:05:43
    now.GetDateTimeFormats('G')[19]  //2015/09/10 下午 04:05:43
    now.GetDateTimeFormats('G')[20]  //2015.09.10 16:05:43
    now.GetDateTimeFormats('G')[21]  //2015.09.10 16:05:43
    now.GetDateTimeFormats('G')[22]  //2015.09.10 下午 4:05:43
    now.GetDateTimeFormats('G')[23]  //2015.09.10 下午 04:05:43
    now.GetDateTimeFormats('G')[24]  //15-9-10 16:05:43
    now.GetDateTimeFormats('G')[25]  //15-9-10 16:05:43
    now.GetDateTimeFormats('G')[26]  //15-9-10 下午 4:05:43
    now.GetDateTimeFormats('G')[27]  //15-9-10 下午 04:05:43
    now.GetDateTimeFormats('G')[28]  //15/9/10 16:05:43
    now.GetDateTimeFormats('G')[29]  //15/9/10 16:05:43
    now.GetDateTimeFormats('G')[30]  //15/9/10 下午 4:05:43
    now.GetDateTimeFormats('G')[31]  //15/9/10 下午 04:05:43
    now.GetDateTimeFormats('G')[32]  //15.9.10 16:05:43
    now.GetDateTimeFormats('G')[33]  //15.9.10 16:05:43
    now.GetDateTimeFormats('G')[34]  //15.9.10 下午 4:05:43
    now.GetDateTimeFormats('G')[35]  //15.9.10 下午 04:05:43
    now.GetDateTimeFormats('G')[36]  //15/09/10 16:05:43
    now.GetDateTimeFormats('G')[37]  //15/09/10 16:05:43
    now.GetDateTimeFormats('G')[38]  //15/09/10 下午 4:05:43
    now.GetDateTimeFormats('G')[39]  //15/09/10 下午 04:05:43
    now.GetDateTimeFormats('M')[0]  //9月10日
    now.GetDateTimeFormats('m')[0]  //9月10日
    now.GetDateTimeFormats('O')[0]  //2015-09-10T16:05:43.6134074+08:00
    now.GetDateTimeFormats('o')[0]  //2015-09-10T16:05:43.6134074+08:00
    now.GetDateTimeFormats('R')[0]  //Thu, 10 Sep 2015 16:05:43 GMT
    now.GetDateTimeFormats('r')[0]  //Thu, 10 Sep 2015 16:05:43 GMT
    now.GetDateTimeFormats('s')[0]  //2015-09-10T16:05:43
    now.GetDateTimeFormats('t')[0]  //16:05
    now.GetDateTimeFormats('t')[1]  //16:05
    now.GetDateTimeFormats('t')[2]  //下午 4:05
    now.GetDateTimeFormats('t')[3]  //下午 04:05
    now.GetDateTimeFormats('T')[0]  //16:05:43
    now.GetDateTimeFormats('T')[1]  //16:05:43
    now.GetDateTimeFormats('T')[2]  //下午 4:05:43
    now.GetDateTimeFormats('T')[3]  //下午 04:05:43
    now.GetDateTimeFormats('u')[0]  //2015-09-10 16:05:43Z
    now.GetDateTimeFormats('U')[0]  //2015年9月10日 8:05:43
    now.GetDateTimeFormats('U')[1]  //2015年9月10日 08:05:43
    now.GetDateTimeFormats('U')[2]  //2015年9月10日 上午 8:05:43
    now.GetDateTimeFormats('U')[3]  //2015年9月10日 上午 08:05:43
    now.GetDateTimeFormats('U')[4]  //2015年9月10日,星期四 8:05:43
    now.GetDateTimeFormats('U')[5]  //2015年9月10日,星期四 08:05:43
    now.GetDateTimeFormats('U')[6]  //2015年9月10日,星期四 上午 8:05:43
    now.GetDateTimeFormats('U')[7]  //2015年9月10日,星期四 上午 08:05:43
    now.GetDateTimeFormats('U')[8]  //星期四,2015年9月10日 8:05:43
    now.GetDateTimeFormats('U')[9]  //星期四,2015年9月10日 08:05:43
    now.GetDateTimeFormats('U')[10]  //星期四,2015年9月10日 上午 8:05:43
    now.GetDateTimeFormats('U')[11]  //星期四,2015年9月10日 上午 08:05:43
    now.GetDateTimeFormats('Y')[0]  //2015年9月
    now.GetDateTimeFormats('Y')[1]  //2015.9
    now.GetDateTimeFormats('y')[0]  //2015年9月
    now.GetDateTimeFormats('y')[1]  //2015.9

    GetDateTimeFormats(char)方法中参数有d, D, f, F, g, G, M (or m), O (or o), R (or r), s, t, T, u, U, and Y (or y)

    这个方法实在是太难记住了,可以用DateTime.ToString(string)方法来格式化时间。

    DateTime.ToString(string format)功能更强大,能输出不同格式的日期。


    在这里列出了参数format格式详细用法(来自网络转载)
    =======================
    格式字符 关联属性/说明
    d ShortDatePattern
    D LongDatePattern
    f 完整日期和时间(长日期和短时间)
    F FullDateTimePattern(长日期和长时间)
    g 常规(短日期和短时间)
    G 常规(短日期和长时间)
    m、M MonthDayPattern
    r、R RFC1123Pattern
    s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
    t ShortTimePattern
    T LongTimePattern
    u UniversalSortableDateTimePattern 用于显示通用时间的格式
    U 使用通用时间的完整日期和时间(长日期和长时间)
    y、Y YearMonthPattern

    下表列出了可被合并以构造自定义模式的模式
    =============================
    这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。

    格式模式 说明 :
    d 月中的某一天。一位数的日期没有前导零。
    dd 月中的某一天。一位数的日期有一个前导零。
    ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
    dddd 周中某天的完整名称,在 DayNames 中定义。
    M 月份数字。一位数的月份没有前导零。
    MM 月份数字。一位数的月份有一个前导零。
    MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
    MMMM 月份的完整名称,在 MonthNames 中定义。
    y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
    yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
    yyyy 包括纪元的四位数的年份。
    gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
    h 12 小时制的小时。一位数的小时数没有前导零。
    hh 12 小时制的小时。一位数的小时数有前导零。
    H 24 小时制的小时。一位数的小时数没有前导零。
    HH 24 小时制的小时。一位数的小时数有前导零。
    m 分钟。一位数的分钟数没有前导零。
    mm 分钟。一位数的分钟数有一个前导零。
    s 秒。一位数的秒数没有前导零。
    ss 秒。一位数的秒数有一个前导零。
    f 秒的小数精度为一位。其余数字被截断。
    ff 秒的小数精度为两位。其余数字被截断。
    fff 秒的小数精度为三位。其余数字被截断。
    ffff 秒的小数精度为四位。其余数字被截断。
    fffff 秒的小数精度为五位。其余数字被截断。
    ffffff 秒的小数精度为六位。其余数字被截断。
    fffffff 秒的小数精度为七位。其余数字被截断。
    t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
    tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
    z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
    zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
    zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
    : 在 TimeSeparator 中定义的默认时间分隔符。
    / 在 DateSeparator 中定义的默认日期分隔符。
    % c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
    / c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“//”。

    只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,

    DateTime.ToString( "d") 返回 DateTime 值;“d”是标准短日期模式。
    DateTime.ToString( "%d") 返回月中的某天;“%d”是自定义模式。
    DateTime.ToString( "d ") 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。

    比较方便的是,上面的参数可以随意组合,并且不会出错,多试试,肯定会找到你要的时间格式

    转载请注明出处:

    作者:JesseLZJ
    出处:http://jesselzj.cnblogs.com

  • 相关阅读:
    操作mysql的指令
    linux下安装mongodb
    linux将指令加入开机启动或加入环境变量
    php请求远程url内容方法
    nosql基本了解
    堆排序的实现
    转:导出csv文件数字会自动变科学计数法的解决方法
    jQuery筛选结果等于true的筛选
    转:Jquery的parent和parents(找到某一特定的祖先元素)
    drupal7 带表达式条件的update
  • 原文地址:https://www.cnblogs.com/jesselzj/p/4798360.html
Copyright © 2011-2022 走看看