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!

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Gene
  • 2,178
  • 3
  • 30
  • 50
  • 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?) – lc. Aug 17 '15 at 04: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 - http://stackoverflow.com/questions/1877788/javascript-date-to-c-sharp-via-ajax) and possibly your question is "how to print DateTime as ISO8601 format"... – Alexei Levenkov Aug 17 '15 at 04:30

3 Answers3

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);
    
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
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"));
        }
    }
}
Aditya Singh
  • 9,512
  • 5
  • 32
  • 55
  • newDT.ToString("yyyy-MM-dd HH:mm:ss.fff") was the one that solved the trick. Thank you so much! – Gene Aug 17 '15 at 05:50
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"));
Sky Fang
  • 1,101
  • 6
  • 6