JavaScript Date to C# via Ajax

Javascript date to C# via Ajax

You can use DateTime.ParseExact which allows you to specify a format string to be used for parsing:

DateTime dt = DateTime.ParseExact("Wed Dec 16 00:00:00 UTC-0400 2009",
"ddd MMM d HH:mm:ss UTCzzzzz yyyy",
CultureInfo.InvariantCulture);

Pass a datetime from javascript to c# (Controller)

The following format should work:

$.ajax({
type: "POST",
url: "@Url.Action("refresh", "group")",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
myDate: '2011-04-02 17:15:45'
}),
success: function (result) {
//do something
},
error: function (req, status, error) {
//error
}
});

How to convert Javascript datetime to C# datetime?

First create a string in your required format using the following functions in JavaScript

var date = new Date();
var day = date.getDate(); // yields date
var month = date.getMonth() + 1; // yields month (add one as '.getMonth()' is zero indexed)
var year = date.getFullYear(); // yields year
var hour = date.getHours(); // yields hours
var minute = date.getMinutes(); // yields minutes
var second = date.getSeconds(); // yields seconds

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

Pass this string to codebehind function and accept it as a string parameter.Use the DateTime.ParseExact() in codebehind to convert this string to DateTime as follows,

DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Hope this helps...

date is changing when is sent to controller with ajax

You're sending the data converted to an ISO string. ISO is in UTC. The controller, then, interprets the date value in its own time zone (rather than UTC) because it doesn't know better. Long and short, don't use .toISOString(). Especially, if all you care about is the date part, you're only going to shoot yourself in the foot passing around times as well. Just pass it formatted as YYYY-MM-DD.

ASP.NET Parse DateTime result from ajax call to javascript date

Use convertToJavaScriptDate() function that does this for you:

function convertToJavaScriptDate(value) {
var pattern = /Date\(([^)]+)\)/;
var results = pattern.exec(value);
var dt = new Date(parseFloat(results[1]));
return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}

The convertToJavaScriptDate() function accepts a value in \/Date(ticks)\/ format and returns a date string in MM/dd/yyyy format.

Inside, the convertToJavaScriptDate() function uses a regular expression that represents a pattern /Date\(([^)]+)\)/.

The exec() method accepts the source date value and tests for a match in the value. The return value of exec() is an array. In this case the second element of the results array (results[1]) holds the ticks part of the source date.

For example, if the source value is \/Date(836418600000)\/ then results[1] will be 836418600000.

Based on this ticks value a JavaScript Date object is formed. The Date object has a constructor that accepts the number of milliseconds since 1 January 1970.

Thus dt holds a valid JavaScript Date object.

The convertToJavaScriptDate() function then formats the date as MM/dd/yyyy and returns to the caller.

You can use the convertToJavaScriptDate() function as shown below:

options.success = function (order) {
alert("Required Date : " + convertToJavaScriptDate(order.RequiredDate) + ", Shipped Date : " + convertToJavaScriptDate(order.ShippedDate));
};

Although the above example uses date in MM/dd/yyyy format, you can use other formats also once Date object is constructed.

reference : Link

What is happening when i pass a date string as response to the Jquery Ajax Call from WebMethod

The asp. Net datetime object is different from javascript date object.. Refer this website which describes clearly.

https://www.dotnetodyssey.com/2015/01/17/sending-datetime-jquery-ajax-json-format-asp-net/

How to Get Data from controller with ajax c#

Considering that this is an AJAX request you are likely better off returning the data as a JSON payload. You can do that by modifying your code like so:

public JsonResult StartPage()
{
var date = CommonService.GetCurrentDateTime();
return Json(new { date });
}

this will return a payload to the client that looks something like this:

{
"date": "<the value of GetCurrentDateTime() as a String>"
}

This can then be easily accessed via your JavaScript code like so:

$.ajax({
type: 'GET',
cache: false,
url: "/Login/StartPage",
complete: function (response) {
var dateString = response.date;
if ( !dateString ) {
console.error('A001: date not returned from server');
}

if (dateString.indexOf('GMT') === -1) {
dateString += ' GMT';
}
var date = new Date(dateString);
$('#time-span').text(dateString);
}
});

You should note that .NET has a funky way of serializing timestamps that may not work for JS, you may want to format the date serverside to something more understandable for JavaScript (see then options for DateTime.ToString())

Javascript Date To C# Webservice Error

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"));


Related Topics



Leave a reply



Submit