简体   繁体   English

将不寻常的字符串转换为日期时间

[英]Convert unusual string into date time

I have a system that outputs dates in the format "1{yy}{MM}{dd}" and I am trying to find a good way to parse it back into a real date. 我有一个以“ 1 {yy} {MM} {dd}”格式输出日期的系统,并且我试图找到一种很好的方法将其解析回实际日期。 At the moment I am using this: 目前,我正在使用此:

  var value = "1110825";
  var z = Enumerable.Range(1,3).Select(i => int.Parse(value.Substring(i, 2))).ToList();
  var d = new DateTime(2000 + z[0], z[1], z[2]);

but I'm sure there's a cleaner/more efficient way to do it? 但是我敢肯定有一种更清洁/更有效的方法吗?

I've tried DT.ParseExact, but can't find a suitable format string to use. 我已经尝试过DT.ParseExact,但是找不到合适的格式字符串来使用。

This works for me: 这对我有用:

using System;
using System.Globalization;

public class Test
{
    static void Main()
    {
        var value = "1110825";
        DateTime dt = DateTime.ParseExact(value, "1yyMMdd",
                                          CultureInfo.InvariantCulture);
        Console.WriteLine(dt);
    }
}

(You may want to use TryParseExact of course, if you need to cope with invalid data in any way other than with an exception.) (如果您需要以除异常之外的任何其他方式来处理无效数据,则当然可以使用TryParseExact 。)

A slight variation of this is the format pattern of "'1'yyMMdd" - note the apostrophes round the 1. That quotes the 1 to force it to be treated as a "literal" in the pattern. 这种格式的细微变化是“'1'yyMMdd”的格式模式-注意1周围的撇号。引用1强制将其视为模式中的“文字”。 It's not important in this case, but if you actually had values such as "y110825" then you'd want to quote the y to make sure it wasn't treated as part of a year specifier. 在这种情况下,这并不重要,但是如果您实际具有诸如“ y110825”的值,则需要引用y以确保不将其视为年份指定符。

you can use DateTime.ParseExact: 您可以使用DateTime.ParseExact:

here is a example for 12/26/1979: 这是1979年12月26日的示例:

    var parseback = DateTime.ParseExact("1791226", "1yyMMdd",
                                        System.Globalization.CultureInfo.CurrentCulture);

Just use this code 只需使用此代码

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DateTimeConvert { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label1.Text= ConvDate_as_str(textBox1.Text); } public string ConvDate_as_str(string dateFormat) { try { char[] ch = dateFormat.ToCharArray(); string[] sps = dateFormat.Split(' '); string[] spd = sps[0].Split('.'); dateFormat = spd[0] + ":" + spd[1]+" "+sps[1]; DateTime dt = new DateTime(); dt = Convert.ToDateTime(dateFormat); return dt.Hour.ToString("00") + dt.Minute.ToString("00"); } catch (Exception ex) { return "Enter Correct Format like <5.12 pm>"; } } private void button2_Click(object sender, EventArgs e) { label2.Text = ConvDate_as_date(textBox2.Text); } public string ConvDate_as_date(string stringFormat) { try { string hour = stringFormat.Substring(0, 2); string min = stringFormat.Substring(2, 2); DateTime dt = new DateTime(); dt = Convert.ToDateTime(hour+":"+min); return String.Format("{0:t}", dt); ; } catch (Exception ex) { return "Please Enter Correct format like <0559>"; } } } } 

Use DateTime.ParseExact(string s, string format, IFormatProvider provider) 使用DateTime.ParseExact(string s, string format, IFormatProvider provider)

an example 一个例子

DateTime.ParseExact("1110825", "1yyMMdd", CultureInfo.InvariantCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM