Convert datetime to the specified format

Source: Internet
Author: User
Tags dateformat

# Region converts a date to the specified format // <summary> // converts the date to the yyyy-mm-dd format. /// </summary> /// <param name = "NDate"> date </param> /// <returns> returns the String object </returns> public string Date_Format (string NDate) {string sm, sd; int y, m, d; try {y = Convert. toDateTime (NDate ). year; m = Convert. toDateTime (NDate ). month; d = Convert. toDateTime (NDate ). day;} catch {return "";} if (y = 1900) return ""; if (m <10) sm = "0" + Con Vert. toString (m); else sm = Convert. toString (m); if (d <10) sd = "0" + Convert. toString (d); else sd = Convert. toString (d); return Convert. toString (y) + sm + sd ;} # endregion # region converts a date to a specified format // <summary> // converts a date to a yyyy-mm-dd format. /// </summary> /// <param name = "NDate"> date </param> /// <returns> returns the String object </returns> public string Date_Format _ (string NDate) {string sm, sd; int y, m, d; try {y = Co Nvert. toDateTime (NDate ). year; m = Convert. toDateTime (NDate ). month; d = Convert. toDateTime (NDate ). day;} catch {return "";} if (y = 1900) return ""; if (m <10) sm = "0" + Convert. toString (m); else sm = Convert. toString (m); if (d <10) sd = "0" + Convert. toString (d); else sd = Convert. toString (d); return Convert. toString (y) + "-" + sm + "-" + sd;} # endregion # region converts the time to a specified format. // <summary> // /Convert the time To hhmmss format. /// </summary> /// <param name = "NDate"> date </param> /// <returns> returns the String object </returns> public string Time_Format (string NDate) {string sh, sm, se; int hh, mm, ss; try {hh = Convert. toDateTime (NDate ). hour; mm = Convert. toDateTime (NDate ). minute; ss = Convert. toDateTime (NDate ). second;} catch {return "";} sh = Convert. toString (hh); if (sh. length <2) sh = "0" + sh; sm = Convert. toS Tring (mm); if (sm. length <2) sm = "0" + sm; se = Convert. toString (ss); if (se. length <2) se = "0" + se; return sh + sm + se;} # endregion # region returns the excel pathname internal string getExcelFileName () // open the excel file {string name = ""; try {OpenFileDialog openCsv = new OpenFileDialog (); openCsv. filter = "Excel file | *. xls ;*. csv ;*. et "; if (openCsv. showDialog () = DialogResult. OK) {name = openCsv. fileName; ret Urn name ;}} catch (OleDbException ode) {MessageBox. show (ode. toString (); return null;} catch (Exception exc) {MessageBox. show ("file not opened! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. information); Console. writeLine (exc. toString ();} return null;} // open the file # endregion # region returns the time format to yyyymmddhhmmss 20130930074600 internal string changeDateTimeFormat (string cardTime) {string dateFormat = Date_Format (cardTime ); string timeFormat = Time_Format (cardTime); string dateAndTime = dateFormat + timeFormat; return dateAndTime;} # endregion

 

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.