Sanju Rao
Sanju Rao

Reputation: 331

How to convert "2014-07-23 06:00" string to date time in "yyyy-MM-dd-HH.mm.ss.ffffff" format

I have string with value 2014-07-23 06:00. I need to convert this value to DateTime in yyyy-MM-dd-HH.mm.ss.ffffff format. Without converting to string in need to display in yyyy-MM-dd-HH.mm.ss.ffffff

But I am getting below error. Error - string was not recognized as a valid datetime

Below Is my Code. Can any one please help me to fix this one.

using System;
using System.Globalization;

public class Example
{
    public static void Main()
    {
        string format = "yyyy-MM-dd-HH.mm.ss.ffffff" ;
        DateTime result;
        const String Date = "2014-07-23 06:00"; ;

        try
        {
            result = DateTime.ParseExact(Date, format,
                CultureInfo.InvariantCulture);
            Console.WriteLine("{0} converts to {1}.", format, result.ToString());
        }
        catch (FormatException)
        {
            Console.WriteLine("{0} is not in the correct format.", format);
        }
    }
}

Upvotes: 0

Views: 2422

Answers (4)

user11909
user11909

Reputation: 1265

VB.NET-Code:

    Dim format As String = "yyyy-MM-dd-HH.mm.ss.ffffff"
    Dim resultString As String = String.Empty
    Dim inputString As String = "2014-07-23 06:00"

    resultString = DateTime.ParseExact(inputString, "yyyy-MM-dd HH:mm", System.Globalization.CultureInfo.InvariantCulture).ToString(format)

Upvotes: 0

dav_i
dav_i

Reputation: 28107

You need to parse it using the format it exists in, then call ToString with the destination format. Additionally you don't want to use a try-catch especially when there is a TryX method available:

var input = "2014-07-23 06:00";

var inputFormat = "yyyy-MM-dd HH:mm";    
var outputFormat = "yyyy-MM-dd-HH.mm.ss.ffffff";

DateTime dateTime;
if (DateTime.TryParseExact(
        input, 
        inputFormat, 
        null, 
        System.Globalization.DateTimeStyles.None, 
        out dateTime))
{
    Console.Write(
        "{0} converts to {1}", 
        inputFormat, 
        dateTime.ToString(outputFormat));
}
else
{
    Console.Write("{0} is not the correct format", inputFormat);
}

Upvotes: 3

Dgan
Dgan

Reputation: 10285

try this:

string format = "yyyy-MM-dd-HH.mm.ss.ffffff";
            DateTime result;
            const String Date = "2014-07-23 06:00"; ;

            try
            {

                DateTime datDateStarted;
                DateTime.TryParseExact(Date, new string[] { "yyyy-MM-dd HH:ss" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out datDateStarted);
                Console.WriteLine(datDateStarted);
            }
            catch (FormatException)
            {
                Console.WriteLine("{0} is not in the correct format.", format);
            }

Upvotes: 0

zappasan
zappasan

Reputation: 82

Looking at this answer I think you can use this line of code:

DateTime myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",
                                       System.Globalization.CultureInfo.InvariantCulture)

Upvotes: -2

Related Questions