Summary of time calculation methods in C #

Source: Internet
Author: User
Tags time zones

These days to do a time data on the statistics of the page, found that some things are still relatively used, and now summarized as follows.

DateTime dt = DateTime.Now; Current time

DateTime startweek = dt. AddDays (1-convert.toint32 (dt.  Dayofweek.tostring ("D"))); This Monday

DateTime Endweek = startweek.adddays (6); This Sunday

DateTime startmonth = dt. AddDays (1-DT.  Day); Early this month

DateTime endmonth = startmonth.addmonths (1).  AddDays (-1); Month End

DateTime endmonth = startmonth.adddays (dt. AddMonths (1)-DT).  DAYS-1); Month End

Note: The actual statistics above are not very accurate, why do you say, for example, I want to count the data of the day this month, for example, today is: 2013-01-16 17:20:37 so the last month's end is 2013-01-31 17:20:37, then this is said 2013-01-31 17:20:37---2013-01-31 24:00:00 This period of time is not counted in, so the best is based on DateTime.Now.Date; This function gets +1 days of the day, through the Where condition < The first date of the next month will be counted.

DateTime startquarter = dt. AddMonths (0-(dt. (Month-1)%3). AddDays (1-DT.  Day); At the beginning of the quarter

DateTime Endquarter = startquarter.addmonths (3).  AddDays (-1); At the end of the quarter

DateTime startyear =new datetime (dt.  Year, 1, 1); Early this year

DateTime endyear =new datetime (dt.  Year, 12, 31); End of year

As for yesterday, tomorrow, last week, last month, last quarter, last year and so on, as long as adddays (), AddMonths (), AddYears () These methods can be combined. Use of DateTime in C #//If you do not understand, look at the Chinese way of showing the day of the week should understand

Since DayOfWeek returns the number of days of the week, we want to convert it into Chinese characters to facilitate our reading, some people may use switch to a control, in fact, not so troublesome

String[] Day =newstring[] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

String week = Day[convert.toint32 (DateTime.Now.DayOfWeek.ToString ("D"))]. ToString (); Last week, the same thing, a week is 7 days, last week is this week minus 7 days, next week is also the same DateTime.Now.AddDays (Convert.ToInt32 (1-convert.toint32 (DateTime.Now.DayOfWeek        ))-7); Last Monday

DateTime.Now.AddDays (Convert.ToInt32 (1-convert.toint32 (DateTime.Now.DayOfWeek))-7).     AddDays (6);        Last weekend (Sunday)//next week DateTime.Now.AddDays (Convert.ToInt32 (1-convert.toint32 (DateTime.Now.DayOfWeek)) +7); Next Monday

DateTime.Now.AddDays (Convert.ToInt32 (1-convert.toint32 (DateTime.Now.DayOfWeek)) +7).      AddDays (6); Next weekend//This month, a lot of people will say the first day of this month is definitely number 1th, the last day is the next month, minus another day. Of course, this is the right//general wording

DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"; First day

DateTime.Parse (DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"). AddMonths (1). AddDays (-1). ToShortDateString ();//Last day//skillfully formatted with ToString characters in C # is easier

DateTime.Now.ToString ("yyyy-mm-01");

DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (1). AddDays (-1). ToShortDateString (); Last month, minus one month

DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (-1). ToShortDateString ();

DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString (); Next month, add a month.

DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (1). ToShortDateString ();

DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). AddMonths (2). AddDays (-1). ToShortDateString (); After 7 days

DateTime.Now.Date.ToShortDateString (); DateTime.Now.AddDays (7). ToShortDateString (); 7 days ago

DateTime.Now.AddDays (-7). ToShortDateString ();

DateTime.Now.Date.ToShortDateString (); This year, formatting with ToString's characters makes it easy to figure out 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, a quarter of three months//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)). AddDays (; Similarly, the last day of the quarter is the first day of the next quarter minus one DateTime.Parse (DateTime.Now.AddMonths (3-(( ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString (); Next quarter, I believe you all know .... Call it

DateTime.Now.AddMonths (3-(( ToString ("yyyy-mm-01");

DateTime.Parse (DateTime.Now.AddMonths (6-(( ToString ("yyyy-mm-01")). AddDays (-1). ToShortDateString (); Last quarter DateTime.Now.AddMonths (( AddDays (; DateTime.Now.AddMonths (0-(( AddDays ( AddDays (-1). ToShortDateString ();

√ String conversion to date type (C #)

such as: "20100101" converted into a date type?

"20100101" converted to int type How to convert??

1. DateTime dt=convert.todatetime ("20100101"). Substring (0,4) + "-" + "20100101". Substring (4,2) + "-" + "20071107". Substring (6,2)); int I=convert.toint32 ("20100101");

2, Convert.todatetime, DateTime.Parse ()

3, String str = "20100101"; DateTime dt = datetime.parseexact (str, "YYYYMMDD", null); int i; Int. TryParse (str, out i);

4. Define a DateTimePicker object, then assign the string you want to convert to the Text property of the DateTimePicker object, and then datetimepicker the value of the object is the date and time that you need. Value values also have Minite,second properties, which can be obtained when, minutes, seconds, and Hao seconds equivalent.


Calculate time in London, and convert to Beijing time:

 private static SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH);  String startTime = formatter.format(new Date());//请求时间

View all the time zones supported by the current system, mainly with standardname,//but ToString () will show the difference in timing as well as the city, can help manual judgment//foreach (Var z in Timezoneinfo.getsystemtimezones ()) {// Console.WriteLine ($ "{Z.standardname}-${z}");//}//Get UK time zone TimeZoneInfo Zone = Timezoneinfo.findsystemtimezonebyid ("GMT Standard Time"); Console.WriteLine (zone); Console.WriteLine (DateTime.Now.Kind); Localvar gtmtime = Timezoneinfo.converttime (DateTime.Now, timezoneinfo.local, zone); Console.WriteLine (gtmtime); var d = new DateTime (9, 1, 0, 0, DATETIMEKIND.UTC); Console.WriteLine (d.kind); Utcconsole.writeline (Timezoneinfo.converttime (d, TIMEZONEINFO.UTC, Zone));

Summary of time calculation methods in C #

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: 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.