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=;i<strs.Length;i++)
{
cut = strs.Substring(i,); //截取一个字符
if (cut.Contains("年")) //当前字符是年字
{
//查找月字
for (int ii = i; ii < strs.Length; ii++)
{
cut = strs.Substring(ii, ); //截取一个字符
if (cut.Contains("月")) //当前字符是月字
{
//包含月字
Mouthbl = true;
var x = i;
var y = ii;
if (y-x<=&& y - x > ) //年月,a年a月,aa年aa月
{
//截取字符串
if (i>=)
{
var year = strs.Substring(i-,); //截取4个
//验证该字符串是否是数字(略)
var mouth = strs.Substring(ii - , ).Replace("年",""); //截取月
var day = "";
//截取日
if (strs.Length-(ii+)>=) //xx月xx日
{
day = strs.Substring(ii,).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 = ; i < strs.Length; i++)
{
cut = strs.Substring(i, ); //截取一个字符
if (cut.Contains("年")) //当前字符是年字
{
//查找月字
for (int ii = i; ii < strs.Length; ii++)
{
cut = strs.Substring(ii, ); //截取一个字符
if (cut.Contains("月")) //当前字符是月字
{
//包含月字
Mouthbl = true;
var x = i;
var y = ii;
if (y - x <= && y - x > ) //年月,a年a月,aa年aa月
{
//截取字符串
if (i >= )
{
var year = strs.Substring(i - , ); //截取4个
//验证该字符串是否是数字(略)
var mouth = strs.Substring(ii - , ).Replace("年", ""); //截取月
if (mouth.Length == ) //位数为1补0
{
mouth = "" + mouth;
}
var day = "";
//截取日
if (strs.Length - (ii + ) >= ) //xx月xx日
{
day = strs.Substring(ii, ).Replace("月", "");
if (day.Length==)////位数为1补0
{
day = "" + day;
}
}
result = year+"#" + mouth + "#" + day;
break; }
} }
}
if (Mouthbl)
{
break; //包含月
}
}
}
return result;
}
}
}
C# 处理年月日提取时间的更多相关文章
- cmd提取时间格式(小时)问题以及Windows系统语言判断
你在这里看到了我的现在的时间是01:15,没错正在做个开发,本来好好的,结果一运行,直接报错: 这里就是时间中的获取小时出了问题,之前23点那会已经调试通过了,过那时是没有问题的,那么这时发生了什么? ...
- NLP入门(十一)从文本中提取时间
在我们的日常生活和工作中,从文本中提取时间是一项非常基础却重要的工作,因此,本文将介绍如何从文本中有效地提取时间. 举个简单的例子,我们需要从下面的文本中提取时间: 6月28日,杭州市统计局权 ...
- datetime 2017-10-21 10:09:02.560 转年月日的时间类型
sql语句时间转年月日格式: 适用于多种时间格式 select REPLACE(STUFF(CONVERT(char(10), REPLACE(CONVERT(varchar(10),'2017-1 ...
- Java获取当前时间年月日、时间格式化打印、字符串转日期
package com.sysc.simple; import java.text.ParseException; import java.text.SimpleDateFormat; import ...
- MYSQL 提取时间中的信息的 4 方法
方法 1. year(),month(),day() 方法 2. dayofweek(),dayofmonth(),dayofyear(); 方法 3. hour(),minute(),second( ...
- js HTML 年月日星期 时间的
效果格式:2019 年 04 月 13 日 星期六 13 : 24 : 49上面的类名有多余的 因为这是之前项目中的 方便以后使用 <!DOCTYPE html> <html> ...
- scala转换date提取年月日时分秒
从数据库提取出来的时间为 String 格式,现在需要转换为 date 并提取出里面的 小时 时间段: import java.text.SimpleDateFormat import java.ut ...
- excel函数提取身份证出生日期,分离日期时间的日期和时间
1.提取身份证出生日期 =1*TEXT(MID(H13,7,8),"0-00-00")用MID函数提取表示日期的位数,再用text函数转换为格式1998-6-21格式的文本,再通过 ...
- 解析Linux内核的基本的模块管理与时间管理操作---超时处理【转】
转自:http://www.jb51.net/article/79960.htm 这篇文章主要介绍了Linux内核的基本的模块管理与时间管理操作,包括模块加载卸载函数的使用和定时器的用法等知识,需要的 ...
随机推荐
- c语言中 %p的含义【转】
本文转载自:http://blog.csdn.net/creat2012/article/details/44224217 今天看到了一个%p,表示没有看到过. = =.学习了. 格式控制符“%p”中 ...
- YTU 1009: University
1009: University 时间限制: 1000 Sec 内存限制: 64 MB 提交: 44 解决: 24 题目描述 在大学里,很多单词都是一词多义,偶尔在文章里还要用引申义.这困扰Red ...
- 自定义View分类与流程
自定义View分类与流程(进阶篇)## 转载出处: http://www.gcssloop.com/customview/CustomViewProcess/ 自定义View绘制流程函数调用链(简化版 ...
- Ubuntu+anaconda环境里安装opencv
在Ubuntu的Anaconda环境下安装OpenCV比较方便,直接在终端中输入以下命令: conda install --channel https://conda.anaconda.org/men ...
- I.MX6 MAC Address 导致的系统崩溃
/**************************************************************************** * I.MX6 MAC Address 导致 ...
- [HEOI 2012] 采花
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2743 [算法] 首先预处理nxt[]数组 , 其中 , nxt[i]表示下一个和i号 ...
- 从0开始学习Hadoop(2)安装JDK以及设置SSH
安装JDK 使用ppa/源方式安装 1.添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装oracl ...
- 使用AngelaSmith.产生测试数据
1.安装库程序包.打开NUGET库程序包管理器控制台:输入 Install-Package AngelaSmith -Version 1.0.1 //1.1.1版本可能有 ...
- ARC102 C~D
C: 枚举中间点,计算两边点差值,把个数乘起来即可 #include<iostream> #include<cstdio> #include<algorithm> ...
- JS开发备忘笔记-- Javascript中document.execCommand()的用法
document.execCommand()方法处理Html数据时常用语法格式如下:document.execCommand(sCommand[,交互方式, 动态参数]) 其中:sCommand为指令 ...