zoukankan      html  css  js  c++  java
  • 三种方法将字符串格式化为日期

    一:DateTime.ParseExact方式,截图

    二:代码

    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
                 参数s
                    类型:System.String
                    包含要转换的日期和时间的字符串。
                    format
                    类型:System.String
                    用于定义所需的 s 格式的格式说明符。 有关更多信息,请参见“备注”一节。 
                    provider
                    类型:System.IFormatProvider
                    一个对象,提供有关 s 的区域性特定格式信息。
                 返回值
                    类型:System.DateTime
                    一个对象,它等效于 s 中包含的日期和时间,由 format 和 provider 指定。
                 */
                #region 针对Windows 7系统
                string s = string.Format("{0}/{1}/{2}",//得到日期字符串
                    txt_Year.Text, txt_Month.Text, txt_Day.Text);
                DateTime P_dt = DateTime.ParseExact(//将字符串转换为日期格式
                    s, "yyyy/MM/dd", null);
                #endregion
                //#region 针对Windows XP或者2003系统
                //string s = string.Format("{0}{1}{2}",//得到日期字符串
                //    txt_Year.Text, txt_Month.Text, txt_Day.Text);
                //DateTime P_dt = DateTime.ParseExact(//将字符串转换为日期格式
                //    s, "yyyyMMdd", null);
                //#endregion
                MessageBox.Show("输入的日期为: "//弹出消息对话框
                    + P_dt.ToLongDateString(), "提示!");
            }
        }
    }

    三:DateTime.ToString格式化日期,截图

    四:代码

    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();
            }
            /*
                  参数format格式详细用法 
                 格式字符 关联属性/说明 
                 d ShortDatePattern 
                 D LongDatePattern 
                 f 完整日期和时间(长日期和短时间) 
                 F FullDateTimePattern(长日期和长时间) 
                 g 常规(短日期和短时间) 
                 G 常规(短日期和长时间) 
                 m、M MonthDayPattern 
                 r、R RFC1123Pattern 
                 s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601) 
                 t ShortTimePattern 
                 T LongTimePattern 
                 u UniversalSortableDateTimePattern 用于显示通用时间的格式 
                 U 使用通用时间的完整日期和时间(长日期和长时间) 
                 y、Y YearMonthPattern 
             */
            private void btn_GetTime_Click(object sender, EventArgs e)
            {
                lab_time.Text =
                    DateTime.Now.ToString("d") + "
    " +//使用指定格式的字符串变量格式化日期字符串
                    DateTime.Now.ToString("D") + "
    " +
                    DateTime.Now.ToString("f") + "
    " +
                    DateTime.Now.ToString("F") + "
    " +
                    DateTime.Now.ToString("g") + "
    " +
                    DateTime.Now.ToString("G") + "
    " +
                    DateTime.Now.ToString("R") + "
    " +
                    DateTime.Now.ToString("y") + "
    " +
                    "当前系统时间为:"+DateTime.Now.ToString(//使用自定义格式格式化字符串
                    "yyyy年MM月dd日 HH时mm分ss秒");
            }
        }
    }

    五:Convert.ToDateTime方式,截图

    六:代码

    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)
            {
                /*参数
                value
                类型:System.String
                日期和时间的字符串表示形式。
                返回值
                类型:System.DateTime
                value 的值的日期和时间等效项,如果 value 为 null,则为 DateTime.MinValue 的日期和时间等效项。
                 */
                string P_DateTime=string.Format("{0}/{1}/{2}",//得到日期字符串
                    txt_Year.Text, txt_Month.Text, txt_Day.Text);
                DateTime P_dt = Convert.ToDateTime(P_DateTime);
                MessageBox.Show("输入的日期为: "//弹出消息对话框
                    + P_dt.ToLongDateString(), "提示!");
            }
        }
    }
  • 相关阅读:
    《编写可维护的JavaScript》读书笔记
    第十四天:还是看代码
    第十三天:过了一遍rt_thread,看代码架构
    第十二天:rt_thread系统
    第十一天:要做stm32了
    第十天:没太专注工作
    第九天:rtc问题查找与测试
    第八天:android编译环境搭建
    第七天:终于看到板子了
    第六天和周末:感慨下这周
  • 原文地址:https://www.cnblogs.com/hongmaju/p/3768257.html
Copyright © 2011-2022 走看看