zoukankan      html  css  js  c++  java
  • Easy String to DateTime, DateTime to String and Formatting

    String to DateTime

     // String to DateTime
     String MyString;
     MyString = "1999-09-01 21:34 PM";
     //MyString = "1999-09-01 21:34 p.m.";  //Depends on your regional settings
    
     DateTime MyDateTime;
     MyDateTime = new DateTime();
     MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",
                                      null);

    DateTime to String

     //DateTime to String
     MyDateTime = new DateTime(1999, 09, 01, 21, 34, 00);
     String MyString;
     MyString = MyDateTime.ToString("yyyy-MM-dd HH:mm tt");

    Format String For Dates

    Your format string is your most important key. In most of my projects, I make it a constant and then refer to the constant value in my code. 

    The following is the most commonly used format characters:

    d - Numeric day of the month without a leading zero.
    dd - Numeric day of the month with a leading zero.
    ddd - Abbreviated name of the day of the week.
    dddd - Full name of the day of the week.
    
    f,ff,fff,ffff,fffff,ffffff,fffffff - 
        Fraction of a second. The more Fs the higher the precision.
    
    h - 12 Hour clock, no leading zero.
    hh - 12 Hour clock with leading zero.
    H - 24 Hour clock, no leading zero.
    HH - 24 Hour clock with leading zero.
    
    m - Minutes with no leading zero.
    mm - Minutes with leading zero.
    
    M - Numeric month with no leading zero.
    MM - Numeric month with a leading zero.
    MMM - Abbreviated name of month.
    MMMM - Full month name.
    
    s - Seconds with no leading zero.
    ss - Seconds with leading zero.
    
    t - AM/PM but only the first letter. 
    tt - AM/PM ( a.m. / p.m.)
    
    y - Year with out century and leading zero.
    yy - Year with out century, with leading zero.
    yyyy - Year with century.
    
    zz - Time zone off set with +/-.

    摘自:http://www.codeproject.com/Articles/14743/Easy-String-to-DateTime-DateTime-to-String-and-For

  • 相关阅读:
    将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片
    <bean:wirte>的用法
    bean标签库Struts标签库
    <bean:write/>标签的format属性
    ByteArrayOutputStream和ByteArrayInputStream详解
    <bean:write>的用法
    FLASH寿命测试!
    波特率,kbps,Mbps含义
    printf()函数是格式化输出!
    Qt学习Qt中的C++
  • 原文地址:https://www.cnblogs.com/bwangff/p/4277024.html
Copyright © 2011-2022 走看看