zoukankan      html  css  js  c++  java
  • C# 处理年月日提取时间

    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 ExtractTime
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //--
                string[] strlist = {"","","" };
                string strs = "日期:    2018年07月31日"; //xx年xx月xx日
                strs = textBox1.Text;
    
    
                var  test =  ff(strs);
                string[] temp = test.Split(new char[] { '#'}, StringSplitOptions.RemoveEmptyEntries);
    
                strs = strs.Replace(" ",""); //日期:2018年07月31日
                var cut = "";bool Mouthbl = false;
                //查询第一个年字所在位置
                for (int i=0;i<strs.Length;i++)
                {
                    cut = strs.Substring(i,1); //截取一个字符
                    if (cut.Contains("")) //当前字符是年字
                    {
                        //查找月字
                        for (int ii = i; ii < strs.Length; ii++)
                        {
                            cut = strs.Substring(ii, 1); //截取一个字符
                            if (cut.Contains("")) //当前字符是月字
                            {
                                //包含月字
                                Mouthbl = true;
                                var x = i;
                                var y = ii;
                                if (y-x<=3&& y - x > 1) //年月,a年a月,aa年aa月
                                {
                                    //截取字符串
                                    if (i>=4)
                                    {
                                        var year =  strs.Substring(i-4,4); //截取4个
                                        //验证该字符串是否是数字(略)
                                        var mouth = strs.Substring(ii - 2, 2).Replace("",""); //截取月
                                        var day = "";
                                        //截取日
                                        if (strs.Length-(ii+1)>=3) //xx月xx日
                                        {
                                          day = strs.Substring(ii,3).Replace("","");
                                        }
                                        textBox2.Text = year + mouth + day;
                                      
                                        break;
    
                                    }
                                }
                               
                            }
                        }
                        if (Mouthbl)
                        {
                            break; //包含月
                        }
                    }
                }
    
    
    
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
    
                // Newmode.CompileDate = Convert.ToDateTime(CompileDate); //时间 字符串转时间
                textBox1.Text = "";
                textBox2.Text = "";
            }
            public string ff(string strs)
            {
                string result = "";
    
                strs = strs.Replace(" ", ""); //日期:2018年07月31日
                var cut = ""; bool Mouthbl = false;
                //查询第一个年字所在位置
                for (int i = 0; i < strs.Length; i++)
                {
                    cut = strs.Substring(i, 1); //截取一个字符
                    if (cut.Contains("")) //当前字符是年字
                    {
                        //查找月字
                        for (int ii = i; ii < strs.Length; ii++)
                        {
                            cut = strs.Substring(ii, 1); //截取一个字符
                            if (cut.Contains("")) //当前字符是月字
                            {
                                //包含月字
                                Mouthbl = true;
                                var x = i;
                                var y = ii;
                                if (y - x <= 3 && y - x > 1) //年月,a年a月,aa年aa月
                                {
                                    //截取字符串
                                    if (i >= 4)
                                    {
                                        var year = strs.Substring(i - 4, 4); //截取4个
                                        //验证该字符串是否是数字(略)
                                        var mouth = strs.Substring(ii - 2, 2).Replace("", ""); //截取月
                                        if (mouth.Length == 1) //位数为1补0
                                        {
                                            mouth = "0" + mouth;
                                        }
                                        var day = "";
                                        //截取日
                                        if (strs.Length - (ii + 1) >= 3) //xx月xx日
                                        {
                                            day = strs.Substring(ii, 3).Replace("", "");
                                            if (day.Length==1)////位数为1补0
                                            {
                                                day = "0" + day; 
                                            }
                                        }
                                        result = year+"#" + mouth + "#" + day;
                                        break;
    
                                    }
                                }
    
                            }
                        }
                        if (Mouthbl)
                        {
                            break; //包含月
                        }
                    }
                }
                return result;
            }
        }
    }
  • 相关阅读:
    docker使用
    window版docker安装及配置
    mysql命令
    xshell
    git 命令
    分页器原理
    PCL-Kinfu编译手册
    cmake-add_definitions
    cmake-include_directories
    cmake-source_group
  • 原文地址:https://www.cnblogs.com/enych/p/11084116.html
Copyright © 2011-2022 走看看