本文介绍了将日期格式ddmmyy(251210)转换为yyyymmdd(20101225).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,其中包含值= 251210(ddmmyy)

我想将此值转换为20101225(yyyymmdd)

I have a textbox containing value = 251210 (ddmmyy)

I want to convert this value = 20101225 (yyyymmdd)

推荐答案

string inp = textBox1.Text;
DateTime dt = DateTime.ParseExact(inp, "ddMMyy", System.Globalization.CultureInfo.InvariantCulture);
string outp = dt.ToString("yyyyMMdd");


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 CodeProjectAnswer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            string date = DateTime.Now.ToString("ddMMyy");
            txtDate.Text = date.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string DayMonthYear="";
            string cdate = txtDate.Text.ToString();
            if (cdate.Length > 0)
            {
               string day=cdate.Substring(0, 2);
               string month = cdate.Substring(2, 2);
               string year = cdate.Substring(4, 2);
               if (DateTime.Now.ToString("yy") == year)
               {
                   DayMonthYear = DateTime.Now.ToString("yyyy");
                   if (DateTime.Now.ToString("MM") == month)
                   {
                       DayMonthYear += DateTime.Now.ToString("MM");
                       if (DateTime.Now.ToString("dd") == day)
                       {
                           DayMonthYear += DateTime.Now.ToString("dd");
                           txtConvertDate.Text = DayMonthYear.ToString();
                       }
                   }
               }
            }
        }
    }
}


这篇关于将日期格式ddmmyy(251210)转换为yyyymmdd(20101225).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 01:01