2

I been trying to convert javascript date to c# datetime format to be inserted into microsoft sql database.

This is the codes i currently have:

Javascript side:

var now     = new Date();
now =  now.toUTCString();

C# side:

//time variable is in DateTime Format

Console.WriteLine("Before: "+time);
String timeString = Convert.ToString(time);

Console.WriteLine("Convert: "+timeString);

DateTime newDT =  DateTime.ParseExact(timeString, "yyyy-MM-dd HH:mm:ss:FFF",   CultureInfo.InvariantCulture);

Console.WriteLine("After: "+newDT);

Error Msg:

System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseExact(String s, String format,    DateTimeFormatInfo dtfi, DateTimeStyles style)
at -classfile&method name omiited-(DateTime time)

Debug Info:

 Before: 17/8/2015 11:43:48 AM
 Convert: 17/8/2015 11:43:48 AM

I'm pretty sure that it is the "timeString" that is inside ParseExact is wrong.

Any ideas on how to solve this ? The format of the result i would want would be in this format: 2015-07-27 14:24:23.853 . Thanks!

2
  • Is this in a server method from a web request? If so, what are you getting for data. Is time or timeString supposed to simulate this? Or am I completely missing the mark? (What is the connection between your JS and C#? How do they communicate?) Commented Aug 17, 2015 at 4:03
  • Very unclear what your problem is. It sounds like you've figured out parsing JS date (also your code seem to be not the best one - stackoverflow.com/questions/1877788/…) and possibly your question is "how to print DateTime as ISO8601 format"... Commented Aug 17, 2015 at 4:30

3 Answers 3

2

The toUTCString function on the Date object returns a value in RFC2822 format, such as "Mon, 17 Aug 2015 05:25:53 GMT". That doesn't align with the "yyyy-MM-dd HH:mm:ss:FFF" format you've specified in your C# code.

You have two options. I recommend the second one.

  1. Change the C# code to match the format passed. The "r" standard format matches RFC2822.

    string s = "Mon, 17 Aug 2015 05:25:53 GMT";
    DateTime dt = DateTime.ParseExact(s, "r", CultureInfo.InvariantCulture,
                      DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
    
  2. Use a clearer format on both side. I recommend the ISO8601 format for most things. In JavaScript, use the toISOString method to get a date similar to "2015-08-17T05:25:43.780Z". Then change your C# code to:

    var s = "2015-08-17T05:25:43.780Z";
    DateTime dt = DateTime.ParseExact(s, "yyyy-MM-dd'T'HH:mm:ss.FFFZ",
                      CultureInfo.InvariantCulture,  DateTimeStyles.RoundtripKind);
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your detailed explanation!
1

Try this out:- https://dotnetfiddle.net/wLSaYD

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateParse
{
    class Program
    {
        static void Main(string[] args)
        {
            var time = "17/8/2015 11:43:48 AM";

            Console.WriteLine("Before: " + time);

            DateTime newDT = DateTime.ParseExact(time, "dd/M/yyyy hh:mm:ss tt", null);

            Console.WriteLine("After: "+newDT.ToString("yyyy-MM-dd HH:mm:ss.fff"));
        }
    }
}

1 Comment

newDT.ToString("yyyy-MM-dd HH:mm:ss.fff") was the one that solved the trick. Thank you so much!
0

17/8/2015 11:43:48 AM against format dd/M/yyyy HH:mm:ss tt,and use tostring format

DateTime newDT = DateTime.ParseExact(timeString, "dd/M/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
Console.WriteLine("After: "+newDT.ToString("yyyy-MM-dd HH:mm:ss.fff"));

1 Comment

Hi, i would want to convert to "yyyy-MM-dd HH:mm:ss:FFF", any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.