C # DateTime Method Application Introduction _c# Tutorial

Source: Internet
Author: User
As you work, write a help document that you keep for yourself.
System.DateTime currenttime=new System.DateTime (); Instantiating a DateTime object
The current month day time minute currenttime=system.datetime.now;
Current year int year =currenttime.year;
Current month int month =currenttime.month;
The current day int day =currenttime.day;
=currenttime.hour at current time int;
The current minute int is divided into =currenttime.minute;
Current second int second =currenttime.second;
Current millisecond int Ms =currenttime.millisecond; (Variables available in Chinese)
DateTime.Now.ToString ()///Get the current system time complete date and time
DateTime.Now.ToLongDateString ()///Only display date xxxx xx month xx, one is a long date
DateTime.Now.ToShortDateString ()//Only show date Xxxx-xx-xx one is a short date
DateTime.Now.Date.ToShortDateString ()//Today
DateTime.Now.AddDays (-1). ToShortDateString ()//yesterday
DateTime.Now.AddDays (1). ToShortDateString ()/Tomorrow
Chinese date/month Day time string stry=currenttime.tostring ("F"); Do not show seconds
Chinese date-month string strym=currenttime.tostring ("Y");
Chinese date Month day string strmd=currenttime.tostring ("M");
The current year date format is: 2003-9-23 string strymd=currenttime.tostring ("D");
The current time division format is: 14:24 string strt=currenttime.tostring ("T");
More format see attached 1, 2.

This week (note that every week here starts from Sunday to Saturday)
DateTime.Now.AddDays (Convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek))). ToShortDateString ();
DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek))). ToShortDateString ();
Last week (last week is minus 7 days this week)
DateTime.Now.AddDays (Convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek))-7). ToShortDateString ();
DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek))-7). ToShortDateString ();
Next week (plus 7 days this week)
DateTime.Now.AddDays (Convert.todouble (0-convert.toint16 (DateTime.Now.DayOfWeek)) + 7). ToShortDateString ();
DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek)) + 7). ToShortDateString ();
This month (the first day of this month is number 1th, the last day is the next month, one and a minus another day)
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
--------------------------------------------------------------------------------
Attached 1:datetime type in ToString (), format formatting
Format characters Association Properties/Description
D ShortDatePattern
D Longdatepattern
F Full date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
G General (short date and short time)
G General (short date and long time)
Mm Monthdaypattern
R, R Rfc1123pattern
S Sortabledatetimepattern using local time (based on ISO 8601)
T Shorttimepattern
T Longtimepattern
U Universalsortabledatetimepattern the format used to display Universal Time
U Full date and time to use universal Time (long date and long time)
Y, y Y, y Yearmonthpattern

Appendix 2: The following table lists the patterns that can be merged to construct custom schemas. These patterns are case-sensitive
Format characters Association Properties/Description
D One day of the month. The date of one number does not have a leading zero.
Dd One day of the month. The date of a single number has a leading zero.
Ddd The abbreviated name of the day of the week, defined in Abbreviateddaynames.
dddd The full name of the day of the week, defined in DayNames.
M The number of months. A single number of months does not have a leading zero.
Mm The number of months. A one-digit month has a leading zero.
MMM The abbreviated name of the month, defined in AbbreviatedMonthNames.
MMMM The full name of the month, defined in MonthNames.
Y The year that does not contain eras. If the year that does not contain the era is less than 10, the year is displayed with no leading zeros.
Yy The year that does not contain eras. If the year that does not contain the era is less than 10, the year with leading zeros is displayed.
yyyy A four-digit year that includes the era.
Gg Period or era. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
H 12-hour hours. The number of hours in a single digit does not have a leading zero.
hh 12-hour hours. The number of hours in a single digit has a leading zero.
H 24-hour hours. The number of hours in a single digit does not have a leading zero.
HH 24-hour hours. The number of hours in a single digit has a leading zero.

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.