"Unity" datetime various Time strings

Source: Internet
Author: User
Tags date1

A variety of ways to express time thank you for the great God on the Internet. This piece for the reprinted article

DateTime.Now.ToShortTimeString ()
DateTime dt = DateTime.Now;
Dt. ToString ();//2005-11-5 13:21:25
Dt. Tofiletime (). ToString ();//127756416859912816
Dt. TOFILETIMEUTC (). ToString ();//127756704859912816
Dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25
Dt. Tolongdatestring (). ToString ();//November 5, 2005
Dt. Tolongtimestring (). ToString ();//13:21:25
Dt. ToOADate (). ToString ();//38661.5565508218
Dt. ToShortDateString (). ToString ();//2005-11-5
Dt. Toshorttimestring (). ToString ();//13:21
Dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25
Dt. Year.tostring ();//2005
Dt. Date.tostring ()//2005-11-5 0:00:00
Dt. Dayofweek.tostring ();//saturday
Dt. Dayofyear.tostring ();//309
Dt. Hour.tostring ();//13
Dt.Millisecond.ToString ();//441
Dt. Minute.tostring ();//30
Dt. Month.tostring ();//11
Dt. Second.tostring ();//28
Dt. Ticks.tostring ();//632667942284412864
Dt. Timeofday.tostring ();//13:30:28.4412864
Dt. ToString ();//2005-11-5 13:47:04
Dt. Addyears (1). ToString ();//2006-11-5 13:47:04
Dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04
Dt. AddHours (1.1). ToString ();//2005-11-5 14:53:04
Dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04
Dt. Addmonths (1). ToString ();//2005-12-5 13:47:04
Dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05
Dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10
Dt. Addticks (1000). ToString ();//2005-11-5 13:47:04
Dt.compareto (DT). ToString ();//0
Dt. ADD (?). ToString ()//question mark is a time period
Dt. Equals ("2005-11-6 16:11:04"). ToString ();//false
Dt. Equals (DT). ToString ();//true
Dt. GetHashCode (). ToString ();//1474088234
Dt. GetType (). ToString ();//system.datetime
Dt. GetTypeCode (). ToString ();//datetime
Dt. Getdatetimeformats (' s ') [0]. ToString ();//2005-11-05t14:06:25
Dt. Getdatetimeformats (' t ') [0]. ToString ();//14:06
Dt. Getdatetimeformats (' y ') [0]. ToString ();//November 2005
Dt. Getdatetimeformats (' D ') [0]. ToString ();//November 5, 2005
Dt. Getdatetimeformats (' D ') [1]. ToString ();//2005 11 05
Dt. Getdatetimeformats (' D ') [2]. ToString ();//Saturday 2005 11 05
Dt. Getdatetimeformats (' D ') [3]. ToString ()//Saturday November 5, 2005
Dt. Getdatetimeformats (' M ') [0]. ToString ();//November 5
Dt. Getdatetimeformats (' f ') [0]. ToString ();//November 5, 2005 14:06
Dt. Getdatetimeformats (' g ') [0]. ToString ();//2005-11-5 14:06
Dt. Getdatetimeformats (' R ') [0]. ToString ();//sat, Nov 14:06:25 GMT
String. Format ("{0:d}", DT);//2005-11-5
String. Format ("{0}", dt);//November 5, 2005
String. Format ("{0:f}", dt);//November 5, 2005 14:23
String. Format ("{0:f}", dt);//November 5, 2005 14:23:23
String. Format ("{0:g}", DT);//2005-11-5 14:23
String. Format ("{0:g}", DT);//2005-11-5 14:23:23
String. Format ("{0:m}", dt);//November 5
String. Format ("{0:r}", DT);//sat, Nov-14:23:23 GMT
String. Format ("{0:s}", DT);//2005-11-05t14:23:23
String. Format ("{0:t}", DT);//14:23
String. Format ("{0:t}", DT);//14:23:23
String. Format ("{0:u}", DT);//2005-11-05 14:23:23z
String. Format ("{0:u}", dt);//November 5, 2005 6:23:23
String. Format ("{0:y}", dt);//November 2005
String. Format ("{0}", dt);//2005-11-5 14:23:23
String. Format ("{0:yyyymmddhhmmssffff}", DT);calculates the number of days difference between 2 dates

DateTime dt1 = Convert.datetime ("2007-8-1");
DateTime DT2 = Convert.datetime ("2007-8-15");
TimeSpan span = dt2. Subtract (DT1);
int Daydiff = span. Days + 1; calculate the number of days of a month in a year

int days = Datetime.daysinmonth (2007, 8);
days = 31; add a day to the date and reduce the day

DateTime DT =datetime.now;
Dt. AddDays (1); Add a day
Dt. AddDays (-1);//Reduce one day
Other year methods are similar ... convert date functions in Oracle SQL

To_date ("2007-6-6", ' Yyyy-mm-dd ");
To_date ("2007/6/6", ' Yyyy/mm/dd ");
Like the next set of data, how to find the table contains September records: cggc_stratdate cggc_enddate

2007-8-4 2007-9-5
2007-9-5 2007-9-20
2007-9-22 2007-10-5
SELECT * from TABLE
(To_date (' 2007/9/1 ', ' yyyy/mm/dd ') BETWEEN cggc_stratdate
and Cggc_enddate OR cggc_stratdate >=to_date (' 2007/9/1 ', ' yyyy/mm/dd ')
and Cggc_enddate<=to_date (' 2007/9/30 ', ' yyyy/mm/dd ') "
OR to_date (' 2007/9/30 ', ' yyyy/mm/dd ') BETWEEN cggc_stratdate
and cggc_enddate) Order by Cggc_stratdate ASC
This year, it's easy to figure out the first and last days of the year with the characters of ToString.
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (1). AddDays (-1). ToShortDateString ();
Last year, there's no need to explain.
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (-1). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddDays (-1). ToShortDateString ();
Next year
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (1). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). Addyears (2). AddDays (-1). ToShortDateString ();
This quarter, many people will feel the difficulty here, need to write a long process to judge. In fact, we all know that four quarters a year, a quarter of three months
First we push the date to the first month of the quarter, and the first day of the month is the first day of the quarter.
DateTime.Now.AddMonths (0-((datetime.now.month-1)% 3)). ToString ("yyyy-mm-01");
Similarly, the last day of the quarter is the first day of the next quarter minus a
DateTime.Parse (DateTime.Now.AddMonths (3-(datetime.now.month-1)% 3)). ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();
Next quarter
DateTime.Now.AddMonths (3-((datetime.now.month-1)% 3)). ToString ("yyyy-mm-01");
DateTime.Parse (DateTime.Now.AddMonths (6-(datetime.now.month-1)% 3)). ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();
Last quarter
DateTime.Now.AddMonths ( -3-((datetime.now.month-1)% 3)). ToString ("yyyy-mm-01");
DateTime.Parse (DateTime.Now.AddMonths (0-(datetime.now.month-1)% 3)). ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString ();
How to get the number of days of the month
int M=system.datetime.daysinmonth (system.datetime.now.year,system.datetime.now.month);
Get the actual working day for a month (that is, excluding week 6th)
Call
int days =getdays (System.DateTime.Now));
private int getdays (System.DateTime date1)
{
int M=system.datetime.daysinmonth (date1. Year,date1. Month);
int mm=0;
for (int i=1;i<=m;i++)
{
System.DateTime Date=convert.todatetime (date1. year+ "-" +date1. month+ "-" +i);
Switch (date. DayOfWeek)
{
Case System.DayOfWeek.Monday:
Case System.DayOfWeek.Thursday:
Case System.DayOfWeek.Tuesday:
Case System.DayOfWeek.Wednesday:
Case System.DayOfWeek.Friday:
mm=mm+1;
Break
}
}
return mm;
}

Get a valid weekday between any two dates (excluding weeks 6th) get a valid weekday between any two dates (excluding weeks 6th) #region Get a valid weekday between any two dates (excluding week 6th)

Call
DateTime date1=convert.todatetime ("2005-10-20");
DateTime date2=convert.todatetime ("2005-11-01");
int days =getdays (DATE1,DATE2);
private int getdays (System.DateTime date1,system.datetime date2)
{
String M=datediff (Enumdatecompare.day,date1,date2). ToString ("F0");

        int mm=0;
        for (int i=0;i<=convert.toint32 (m); i++)
        {
            System.DateTime date=convert.todatetime (date1. AddDays (i));
            Switch (date. DayOfWeek)
            {case
                System.DayOfWeek.Monday: Case
                System.DayOfWeek.Thursday:
                case System.DayOfWeek.Tuesday: Case
                System.DayOfWeek.Wednesday: Case
                System.DayOfWeek.Friday:
                    mm=mm+1;
                    break;
            }                
        }
        return mm;
    }
    #endregion

Format output
Format Output #region format output
private void Page_Load (object sender, System.EventArgs e)
{
System.Globalization.DateTimeFormatInfo Mydtfi = new System.Globalization.CultureInfo ("en-us", false). DateTimeFormat;
China uses ZH-CN
DateTime myDT =system.datetime.now;
Response.Write (Mydt.tostring ("F", Mydtfi));
/////*//
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE of associated property, IF any
D 1/3/2002 m/d/yyyy (ShortDatePattern)
D Thursday, January, 2002 dddd, MMMM DD, yyyy (LONGDATEPATTERN)
F Thursday, January, 2002 12:00 AM
F Thursday, January, 2002 12:00:00 AM dddd, MMMM dd, yyyy h:mm:ss TT (Fulldatetimepattern)
G 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
M January MMMM DD (Monthdaypattern)
M January MMMM DD (Monthdaypattern)
R Thu, 2002 00:00:00 GMT ddd, dd MMM yyyy HH ': ' mm ': ' SS ' GMT ' (Rfc1123pattern)
R Thu, 2002 00:00:00 GMT ddd, dd MMM yyyy HH ': ' mm ': ' SS ' GMT ' (Rfc1123pattern)
s 2002-01-03t00:00:00 yyyy '-' mm '-' dd ' T ' HH ': ' mm ': ' SS (Sortabledatetimepattern)
T 12:00 AM h:mm TT (Shorttimepattern)
T 12:00:00 AM h:mm:ss tt (Longtimepattern)
U 2002-01-03 00:00:00z yyyy '-' mm '-' dd HH ': ' mm ': ' SS ' Z ' (Universalsortabledatetimepattern)
U Thursday, January, 2002 8:00:00 AM
Y January, 2002 MMMM, YYYY (Yearmonthpattern)
Y January, 2002 MMMM, YYYY (Yearmonthpattern)
*/
}endregion

Get this week's Saturday and Sunday
Convertdatetoweek#region Convertdatetoweek
public static void Convertdatetoweek (DateTime date,out datetime firstdate,out datetime lastdate)
{
DateTime First=system.datetime.now;
DateTime Last=system.datetime.now;
Switch (date. DayOfWeek)
{Case System.DayOfWeek.Monday:
First=date. AddDays (-1);
Last=date. AddDays (5);
Break
Case System.DayOfWeek.Tuesday:
First=date. AddDays (-2);
Last=date. AddDays (4);
Break
Case System.DayOfWeek.Wednesday:
First=date. AddDays (-3);
Last=date. AddDays (3);
Break
Case System.DayOfWeek.Thursday:
First=date. AddDays (-4);
Last=date. AddDays (2);
Break
Case System.DayOfWeek.Friday:
First=date. AddDays (-5);
Last=date. AddDays (1);
Break
Case System.DayOfWeek.Saturday:
First=date. AddDays (-6);
Last=date;
Break
Case System.DayOfWeek.Sunday:
First=date;
Last=date. AddDays (6);
Break
}
Firstdate=first;
Lastdate=last;
}endregion

Call
DateTime Firstdate=system.datetime.now;
DateTime Lastdate=system.datetime.now;
Convertdatetoweek (date,out firstdate,out lastdate);
Get the current date is the week ordinal of the year
DateTime dt = Convert.todatetime ("2006-05-01");
int weeks = dt. DAYOFYEAR/7 + 1;

Original link http://www.manew.com/blog-1121-845.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.