ImmortalStrawberry
ImmortalStrawberry

Reputation: 6091

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. 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.

Upvotes: 0

Views: 1379

Answers (4)

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>";
            }
        }
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500795

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.)

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. 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.

Upvotes: 9

Random Dev
Random Dev

Reputation: 52280

you can use DateTime.ParseExact:

here is a example for 12/26/1979:

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

Upvotes: 1

Russ Cam
Russ Cam

Reputation: 125498

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

an example

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

Upvotes: 0

Related Questions