admin管理员组

文章数量:1335680

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.

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.

Share Improve this question asked Oct 12, 2016 at 16:53 Pinal DavePinal Dave 5334 gold badges14 silver badges27 bronze badges 5
  • should convert it to timestamp – Steve 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. – Pablo Recalde Commented Oct 12, 2016 at 17:00
  • @r1verside timestamp is timezone safe. unless you want the server time instead of local time – Steve 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. – Pablo Recalde Commented Oct 12, 2016 at 19:05
  • @r1verside op is trying to parse the datetime sent from js time to c# time. – Steve Commented Oct 12, 2016 at 19:08
Add a ment  | 

3 Answers 3

Reset to default 3

mm is for minutes, you want MM for month:

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

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/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.

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.

本文标签: convert javascript date to c datetimeStack Overflow