how can I convert "/Date(1479250800000)/" String to a C# Datetime?
Thanks in advance
Assuming the value inside the brackets is number of ticks:
var datstr = "/Date(1479250800000)/";
long ticks = Convert.ToInt64(datstr.Substring(6, 13));
DateTime date = new DateTime(ticks);
There will be a difference between .Net and javascript tick value:
The JavaScript Date type's origin is the Unix epoch: midnight on 1 January 1970. The .NET DateTime type's origin is midnight on 1 January 0001. If by "ticks" you mean something like "milliseconds since the epoch", you can call ".getTime()
There are 621355968000000000 epoch ticks for javascript from Ist Jan 1900 to Ist Jan 1970. And here 10000 are the ticks per milliseconds.
quoted from here. You will need to correct for this. the last line would look like the following:
DateTime date = new DateTime(ticks * 10000 + 621355968000000000);
You'll get the string "/Date(1479250800000)/" as a date value in javascript if you are sending it from C#. But if you sending back the value to a date field in C# then the value will be deserialized as DateTime value. You don't need to convert it explicitly, just get it in a DateTime field.
Related
The codes below return current UTC datetime in string format.
But i am looking for a function in javascript that return current utc datetime in datetime format.
It seems there is no such that function in javascript.
var dateTime_now = new Date();
var dateTime_now_utc_str = dateTime_now.toUTCString();
alert(dateTime_now_utc_str);
.toISOString() is what you want, not .toUTCString()
You already have the Javascript internal DateTime format in the variable dateTime_now. So I think you do want a string output, but not the string Sun, 05 Dec 2021 06:11:15 GMT, because it contains useless strings like Sun and useful, but non-numerical strings like Dec. I am guessing you do want a string output, but containing digits and separators and no words.
var dateTime_now = new Date();
var dateTime_now_utc_str = dateTime_now.toISOString();
console.log(dateTime_now_utc_str);
// Result: 2021-12-05T06:10:54.299Z
Why?
A detailed explanation of why is given here, by me:
https://stackoverflow.com/a/58347604/7549483
In short, UTC simply means "in the timezone of GMT+0", while ISO is the name of the format yyyy-mm-dd etc. The full name of the date format is ISO 8601.
The server side is in C# and it returns object array list as json format. There is one field in the object model, it is DateTime type, and its value is DateTime.MinValue.
While in the page side, it receives /Date(-62135596800000)/ in string. I guess this is because of the object is serialized. And in javascirpt, I try to convert it back to Date type.
var timeSpan = element.DateModify.replace('Date','').replace('(','').replace(')','').replace(/\//g,'');
console.log(timeSpan);
var d = new Date(parseInt(timeSpan));
console.log(d);
When converted to Date in javascript, its value is 0001-01-01 08:05:43, not 0001-01-01 00:00:00. Why is it?
DateTime.MinValue is 0001-01-01 00:00:00 UTC. And that is indeed 0001-01-01 08:05:43 in your local time zone (whatever that may be, probably Asia/Shanghai or somewhere near that). When you console.log a Date, it displays the date in your local time zone. The Date value is correct. It's just displayed in a different format.
The extra 5 minutes and 43 seconds is because at the year AD 1, time zones have not been standardised, and the local mean time offset from UTC at your location is +08:05:43.
Two simple ways to make it display the UTC time of 00:00:00 is to call toISOString or toUTCString:
console.log(new Date(-62135596800000).toISOString());
console.log(new Date(-62135596800000).toUTCString());
1370131200000
I was using hichart to plot timeline data. in the example the date field in x axis is like this. But my timestamp values are like 1502582400.
It's UNIX time stamp (Wikipedia)
1502582400 is 08/13/2017 # 12:00am (UTC)
You can find how to convert to other formats here.
JavaScript dates are numbers which specify the number of milliseconds since January 1, 1970, 00:00:00.
I want to take date in my module with JSON format and when I am converting my date value to json then it changes the timezone ultimately date gets change for eg
var myDateWithJson=(new Date(2014, 03, 11).toJSON());
alert("Date With Json " +myDateWithJson);
var myDateWithoutJson = new Date(2014,03,11);
alert("Date Without Json " + myDateWithoutJson);
I also gone through covert json without timezone but, I don't think that is better approch
Please guide me for the better approch
In your code:
var myDateWithJson=(new Date(2014, 03, 11).toJSON());
will create a date object for 00:00:00 on the morning of 11 April 2014 (note that months are zero indexed here) in your current locale based on system settings. Calling toJSON returns an ISO 8601 date and time string for the equivalent moment based on UTC.
The date object will have an internal time value that is milliseconds since 1970-01-01T00:00:00Z.
var myDateWithoutJson = new Date(2014,03,11);
That creates a date object for exactly the same moment in time, i.e. with exactly the same time value.
alert("Date Without Json " + myDateWithoutJson);
That calls the toString method of the date object that returns a human readable string representing the date and time in the current locale based on system settings.
So the first is a UTC string, the second is a local string. Both represent the exact same instant in time, and, if converted back to Date objects, will have exactly the same internal time value.
I need to pass javascript date value to vb.net function.
Method iam using now:
convert javascript date to string
store it in hiddenfield
retrieve string from hidden field in server code and parse it using date.parse
the trouble is that the Javascript dateformats
toString() - Sat Apr 4 22:19:00 UTC+0530 2009
toDateString() - Sat Apr 4 2009
toLocaleString() - Saturday, April 04, 2009 10:19:00 PM
doesnt match vb date format. I am getting error that its unparseable.
Thanks in advance for the help
The problem with using ToLocaleString is that you lose timezone info and its obviously locale specific which means you need to parse it with the right culture.
I was thinking:-
DateTime d = DateTime.ParseExact(sInput, "ddd MMM d HH:mm:ss UTCzzzz yyyy" , CultureInfo.InvariantCulture);
But that isn't cross browser compliant (the ECMA spec does not define what toString should actually do).
However we do know that the value of a Javascript Date object is the number of milliseconds from midnight Jan 1, 1970. Hence you could instead store the .valueOf of a date object in your hidden field. Use Int32.Parse on the string first, create a TimeSpan from the that value and add it to a DateTime of Jan 1, 1970 00:00:00 UTC+0000.
int milliseconds = Int32.Parse(inputString);
TimeSpan t = TimeSpan.FromMilliseconds(milliseconds);
DateTime base = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime result = base + t;
Why not instead pass the Javascript date as a string and then convert it to a date type in VB.net.
Public Function ConvertJavaScriptDate(ByVal d as String) As Date
Return Date.Parse(d)
End Function
Another option is to use CType(d,Date). CType is a lexical cast that will try a variety of ways to convert the string value to a Date.
I'm not terribly familiar with the difference between a JavaScript Date and a VB.Net Date in terms of format, but if you post an example I'm sure we can get a basic conversion going.
Since I don't have to worry about culture difference I am going to use toLocaleString().
toLocaleString() parses fine to a string compatible with Date.Parse().
Anyway thanks for posting your replies.
This just a datetime formating issue can you look this post for more details.
How we can resolve the datetime problem shifting the Access DB from production server to live
You can also use DateTime.ParseExact() to tell the VB code exactly what the incoming string should look like.