0

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);
        }
    }
}
Sanju Rao
  • 331
  • 1
  • 6
  • 25

4 Answers4

3

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);
}
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • dateTime is again in 23/07/2014 06:00:00 format actually i need the output in datetime variable to a specified format – Sanju Rao Nov 10 '14 at 10:26
  • `DateTime` itself contains no formatting (e.g. when you're inspecting it via debugger, just the default formatting is shown). Only when you call `ToString` does it become formatted but then it becomes a string so no longer is a `DateTime`. – dav_i Nov 10 '14 at 10:30
  • thank you. So it is confirmed that Date time will always be in 23/07/2014 06:00:00 format. Unless and until its not converted to string. I am i correct? – Sanju Rao Nov 10 '14 at 10:35
  • Saying that `DateTime` is "in a format" is meaningless as it is just a structure for holding date and time information. However, yes, in the debugger it will be displayed in that format. – dav_i Nov 10 '14 at 10:37
  • Without converting to string in need to display in yyyy-MM-dd-HH.mm.ss.ffffff is that possible? – Sanju Rao Nov 10 '14 at 10:57
  • I think you're misunderstanding the concept here - there is no way to display any value of any type without it being converted to a string, this is why all objects have a `ToString` method. When viewing in the debugger, `ToString` is called. If you want to display to a user, you need to call `ToString` and at that point you can choose a format to display it in as shown above. – dav_i Nov 10 '14 at 11:01
0

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);
            }
Dgan
  • 10,077
  • 1
  • 29
  • 51
0

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)
user11909
  • 1,235
  • 11
  • 27
-2

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)
Community
  • 1
  • 1
zappasan
  • 82
  • 10