Instance 365 (8) --------- three methods to format a string as a date

Source: Internet
Author: User
Tags iso 8601

I. DateTime. ParseExact method,

Ii. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace ConvertToString {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void btn_Convert_Click (object sender, EventArgs e) {/* DateTime. parseExac parameter s type: System. string contains the date and time to be converted. Format type: System. String is used to define the format specifiers of the desired s format. For more information, see the "Remarks" section. Provider type: System. IFormatProvider, an object that provides information about the specific format of s. Return Value Type: System. DateTime is an object. It is equivalent to the date and time contained in s, which is specified by format and provider. */# Region for Windows 7 system string s = string. format ("{0}/{1}/{2}", // obtain the date string txt_Year.Text, txt_Month.Text, txt_Day.Text); DateTime P_dt = DateTime. parseExact (// convert the string to the date format s, "yyyy/MM/dd", null ); # endregion // # region for Windows XP or 2003 systems // string s = string. format ("{0} {1} {2}", // obtain the date string // txt_Year.Text, txt_Month.Text, txt_Day.Text); // DateTime P_dt = DateTime. parseExact (// convert string to date format //, "yyyyMMd D ", null); // # endregion MessageBox. Show (" input date: "// pop-up message dialog box + P_dt.ToLongDateString ()," prompt! ");}}}
3. DateTime. ToString format date,

4. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace TmrFormat {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} /* parameter format detailed usage format character Association attributes/description d ShortDatePattern D LongDatePattern f complete date and time (long date and short time) F FullDateTimePattern (long date and long time) g regular (short date and short time) G regular (short date and long time) m, M MonthDayPattern r, R RFC1123Pattern s use local time SortableDateTimePattern (based on ISO 8601) t ShortTimePattern T LongTimePattern u UniversalSortableDateTimePattern is used to display the format of Universal Time U use the complete date and time of Universal Time (long date and long time) y, Y YearMonthPattern */private void btn_GetTime_Click (object sender, EventArgs e) {lab_time.Text = DateTime. now. toString ("d") + "\ n" + // format the DateTime string using the string variable in the specified format. now. toString ("D") + "\ n" + DateTime. now. toString ("f") + "\ n" + DateTime. now. toString ("F") + "\ n" + DateTime. now. toString ("g") + "\ n" + DateTime. now. toString ("G") + "\ n" + DateTime. now. toString ("R") + "\ n" + DateTime. now. toString ("y") + "\ n" + "current system time:" + DateTime. now. toString (// format the string in the custom format "yyyy MM dd HH mm ss seconds ");}}}
V. Convert. ToDateTime mode,

Vi. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace ConvertToString {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void btn_Convert_Click (object sender, EventArgs e) {/* parameter value type: system. string Representation of the date and time. Return value Type: Date and time equivalent of System. DateTime value. If value is null, it is the date and time equivalent of DateTime. MinValue. */String P_DateTime = string. format ("{0}/{1}/{2}", // obtain the date string txt_Year.Text, txt_Month.Text, txt_Day.Text); DateTime P_dt = Convert. toDateTime (P_DateTime); MessageBox. show ("input date:" // pop-up message dialog box + P_dt.ToLongDateString (), "prompt! ");}}}

 

Related Article

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.