C # Format and usage of the current time given by datetime

Source: Internet
Author: User
Tags date1

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, 2005 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, 2005 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 difference in the number of days 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 the month in a year
-----------------------------------------------
int days = Datetime.daysinmonth (2007, 8);
days = 31;
Add one day to date, one day less
-----------------------------------------------
DateTime DT =datetime.now;
Dt. AddDays (1); Add one day
Dt. AddDays (-1);//Decrease One day
Other years method similar ...
Convert Date function in Oracle sql
-----------------------------------------------
To_date ("2007-6-6", "yyyy-mm-dd");
To_date ("2007/6/6", "yyyy/mm/dd");
For the next set of data, how to find a table that 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, formatted with ToString characters we also easily calculate the first and last day of the year
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). ToShortDateString ();
DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")). AddYears (1). AddDays (-1). ToShortDateString ();
//Last year, no more explaining
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, three months a quarter
First we push the date to the first month of the quarter, and then 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 one
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 how many days a month

int M=system.datetime.daysinmonth (system.datetime.now.year,system.datetime.now.month);

Get the actual working day of 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;
}


Obtain a valid working day between any two dates (excluding week 6th) to obtain a valid working day between any two dates (excluding week 6th) #region Get a valid working day between any two dates (excluding weeks 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 with 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
F Thursday, January, 2002 12:00:00 AM dddd, MMMM dd, yyyy h:mm:ss TT (Fulldatetimepattern)
G 1/3/2002 AM
G 1/3/2002 12:00:00 AM
M January MMMM DD (Monthdaypattern)
M January MMMM DD (Monthdaypattern)
R Thu, Jan 2002 00:00:00 GMT ddd, dd MMM yyyy HH ': ' mm ': ' SS ' GMT ' (Rfc1123pattern)
R Thu, Jan 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-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 of the year
DateTime dt = Convert.todatetime ("2006-05-01");
int weeks = dt. DAYOFYEAR/7 + 1;

C # Format and usage of the current time given by datetime

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.