简体   繁体   中英

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

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

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

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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