Sergey Metlov
Sergey Metlov

Reputation: 26311

Send date from client to server

I want to send data from client and create it on server. So:
1) How can I get the total milliseconds count by JavaScript Date object?
2) How can I create .NET DateTime object by total milliseconds count?

Upvotes: 1

Views: 4019

Answers (2)

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

You will have to use AJAX for this. Once you send the d.getTime() as explained by the other answer, parse it like this in your C# code behind:

if (!string.IsNullOrEmpty(Request.Form["milliseconds"]))
{
    long clientSideMS = Int64.Parse(Request.Form["milliseconds"]);
    DateTime past = new DateTime(1970, 1, 1);
    DateTime clientSideDate = past.AddMilliseconds(clientSideMS);
}

After this, clientSideDate will be the date on the client side.

Edit: using jQuery, posting the date is as simple as:

var now = new Date();
var ms = now.getTime();
$.post("Page.aspx", { milliseconds: ms.toString() } );

Upvotes: 2

mowwwalker
mowwwalker

Reputation: 17382

var d = new Date();
alert(d.getMilliseconds()); // for the milliseconds between the current seconds
alert(d.getTime()); // for the milliseconds since Midnight, Jan 1, 1970

Upvotes: 0

Related Questions