0

I am trying to convert javascript date to c# datetime

JavaScript Code

var date = new Date();
    var day = date.getDay();        
    var month = date.getMonth();    
    var year = date.getFullYear();  
    var hour = date.getHours();     
    var minute = date.getMinutes(); 
    var second = date.getSeconds(); 

    // After this construct a string with the above results as below
    var JSDateString = year+ "-" + month + "-" + day + " " + hour + ':' + minute + ':' + second;

C# Code

var JSDateString = "2016-04-02 17:15:45";  // I receive date string via Ajax call in this format

var dt = DateTime.ParseExact(JSDateString , "yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture);

I get invalid datetime format exception. I researched other options in internet but I didn't find any specific answer on how to convert JavaScript datetime to C# datetime.

5
  • should convert it to timestamp Commented Oct 12, 2016 at 16:55
  • @Steve yes, and also have a eye in the possible diference in timezones between client and server in this case. Commented Oct 12, 2016 at 17:00
  • @r1verside timestamp is timezone safe. unless you want the server time instead of local time Commented Oct 12, 2016 at 18:45
  • @Steve timestamp is UTC. But the problem is JavaScript does not know how to handle timezone so if you're displaying dates in a different timezone than the browser timezone, it will give you an incorrect timestamp, because it will not be UTC. Commented Oct 12, 2016 at 19:05
  • @r1verside op is trying to parse the datetime sent from js time to c# time. Commented Oct 12, 2016 at 19:08

3 Answers 3

3

mm is for minutes, you want MM for month:

var dt = DateTime.ParseExact(JSDateString , "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
Sign up to request clarification or add additional context in comments.

Comments

3

This might help with the JavaScript side:

function getDate() {
  var date = new Date(),
    year = date.getFullYear(),
    month = (date.getMonth() + 1).toString(),
    formatedMonth = (month.length === 1) ? ("0" + month) : month,
    day = date.getDate().toString(),
    formatedDay = (day.length === 1) ? ("0" + day) : day,
    hour = date.getHours().toString(),
    formatedHour = (hour.length === 1) ? ("0" + hour) : hour,
    minute = date.getMinutes().toString(),
    formatedMinute = (minute.length === 1) ? ("0" + minute) : minute,
    second = date.getSeconds().toString(),
    formatedSecond = (second.length === 1) ? ("0" + second) : second;
  return year + "-" + formatedMonth + "-" + formatedDay + " " + formatedHour + ':' + formatedMinute + ':' + formatedSecond;
};

View a fiddle here: https://jsfiddle.net/kpduncan/de8j318k/

I had too do something like this when I building an application due to not being allowed to add thrid party JS and needing support back to IE8.

1 Comment

Thanks. Very useful for trailing 0s for parsing on the C# side (fiddle is gone though)
1

As you can see on the MSDN, mm is for minutes (00 - 59) whereas MM is for the month (01 - 12).

var JSDateString = "2016-04-02 17:15:45";
var formatCode = "yyyy-MM-dd HH:mm:ss";

var dt = DateTime.ParseExact(JSDateString , formatCode, CultureInfo.InvariantCulture);

You can see that mm is for minutes because you already use it in your HH:mm:ss.

Comments

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.