How to Convert JavaScript Datetime to C# Datetime

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

Convert JavaScript Date to .NET DateTime

Instead of parsing a textual representation it would be more robust to construct a DateTime from a timestamp instead. To get a timestamp from a JS Date:

var msec = date.getTime();

And to convert msec (which represents a quantity of milliseconds) into a DateTime:

var date = new DateTime(1970, 1, 1, 0, 0, 0, 0); // epoch start
date = date.AddMilliseconds(msec); // you have to get this from JS of course

Convert javascript gettime() to c# datetime

The value that you get from the getTime method is not from the local time, but from the universal time. When you add those milliseconds to 1970-1-1, you get the universal time, not the local time.

Use the ToLocal method to get the DateTime value for the local time:

DateTime fromDate =
seventies.AddMilliseconds(Convert.ToDouble(Reader["FromDateMill"])).ToLocal();

Convert String dateTime of javascript to DateTime C#

Stripe off the end of the string and use this :

var Date = "Tue May 13 2014 00:00:00 GMT+0700";
var FormattedDate = DateTime.ParseExact(Date,"ddd MMM dd yyyy HH:mm:ss
'GMT'zzz",System.Globalization.CultureInfo.InvariantCulture);

Hope this helps you.
Cheers.

Mismatch C# Javascript date/time

This gives me the same result:

new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(1560780000000)

or this

new DateTime(1970, 1, 1).AddMilliseconds(1560780000000).ToLocalTime()

They are not 100% equivalent, but the second one will create a date with kind Unspecified that is assumed to be UTC when you call ToLocalTime().

The difference if you call ToLocalTime() before or after AddMilliseconds() is in Daylight Saving it seems. Depending on the date, same local time will be interpreted differently. So you should always do all calculations in UTC I assume and only convert to local time to check in the end.

The problem was that you have added milliseconds that made it to be in summer when DST would make a difference (I think ).

More details here.

C# Convert datetime to date javascript

I usually use the following function to convert this date format to JS format:

function parseDotNetDate(str) {
var regexp = /\/Date\((\d*)\)\//;
if (!regexp.test(str))
throw new Error('Not a .NET DateTime object.');

return new Date(+str.replace(regexp, '$1'));
}

Get Correct Date In Milliseconds From Javascript To C#

There is a better way to pass the date object to C# and that is using ordinary ISO 8601 standard format (e.g. 2016-01-01T12:00:00.568Z). If you call toJSON() on your date, the binder in C# should automatically convert it to C# datetime with appropriate time zone.

You will be able to see if you have a timzeone mismatch with you milliseconds in javascript easier as well than dealing with raw millisecond number.

You can read more on it here.

How to convert javascript numeric date into C# date (using C#, not javascript!)

If I'm not mistaken, that is a Unix timestamp in milliseconds. 1184050800000 is the timestamp itself, and -0700 is the time zone. This epoch convertor confirms.

Here is some code I've used before for converting Unix timestamps into DateTimes. Be sure to include only the part before -0700:

/// <summary>
/// Converts a Unix timestamp into a System.DateTime
/// </summary>
/// <param name="timestamp">The Unix timestamp in milliseconds to convert, as a double</param>
/// <returns>DateTime obtained through conversion</returns>
public static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp / 1000); // convert from milliseconds to seconds
}

If you encounter Unix timestamps that are in seconds, you just have to remove the / 1000 part of the last line of the code.



Related Topics



Leave a reply



Submit