How to convert JavascriptSerializer serialized DateTime string to Javascript Date object - javascript

After serializing an object with DateTime field with JavaScriptSerializer, I see that the DateTime field looks like this:
EffectiveFrom: "/Date(1355496152000)/"
How can I convert this string to Javascript Date object?

UPDATE: This answer may not be appropriate in all cases. See JD's answer for an elegant solution that is likely better.
You could just "fix" the output from JavaScriptSerializer on the .Net side of things:
JavaScriptSerializer serializer = new JavaScriptSerializer();
var json = serializer.Serialize(this);
json = Regex.Replace(json,#"\""\\/Date\((-?\d+)\)\\/\""","new Date($1)");
return json;
This would change
EffectiveFrom: "/Date(1355496152000)/"
to
EffectiveFrom: new Date(1355496152000)
Which is directly consumable by Javascript
EDIT: update to accommodate negative dates
EDIT: Here is the Regex line for VB folks:
json = Regex.Replace(json, """\\/Date\((-?\d+)\)\\/""", "new Date($1)")
UPDATE 2016.11.20: With a lot more datetime handling in javascript/json behind me, I would suggest changing the regex to something as simple as
json = Regex.Replace(json,#"\""\\/Date\((-?\d+)\)\\/\""","$1");
The resulting value is valid JSON, and can be converted to a Date object on the javascript side.
It is also worth noting that moment.js (http://momentjs.com/docs/#/parsing/) handles this format happily enough.
moment("/Date(1198908717056-0700)/");

There is an answer that may help you:
Parsing Date-and-Times from JavaScript to C#
If you want to parse datetime string to datetime value with javascript you must use "new Date" like this:
var data = new Date("1355496152000");

var obj = { EffectiveFrom: "/Date(1355496152000)/" };
//parse the Date value and replace the property value with Date object:
var dateValue = parseInt(obj.EffectiveFrom.replace(/\/Date\((\d+)\)\//g, "$1"));
obj.EffectiveFrom = new Date(dateValue);

This is a bit of a hack, but the above seemed inelegant for what I'm trying to achieve, so in the object definition I'm serializing, I did this:
/// <summary>Date of the record retention event or document date.
/// </summary>
public string DateOfRetentionEvent;
[ScriptIgnore]
public DateTime RetentionEventDate
{
get
{
return _retentionEventDate;
}
set
{
_retentionEventDate = value;
DateOfRetentionEvent = value.ToShortDateString();
}
}
The point being that, at least in my use case (deserialization never happens), the JSON doesn't really care what C# is doing with the date value. Adding [ScriptIgnore] to the DateTime value and giving an alternative view for the parser to output should do the trick. It does in my case:
{
"DateToDispose": "1/1/2020",
"DateOfRetentionEvent": "10/1/2014",
"FullRetentionCode": "NR+5",
"RetentionEvent": "NR",
"RetentionPeriod": 5
}

Related

Return the date format for date without moment.js

I can't figure out how to build a function that does this
INPUT: dateToDateFormat('16/07/2022') -> OUTPUT: DD/MM/YYYY
INPUT: dateToDateFormat('07/16/2022') -> OUTPUT: MM/DD/YYYY
The input dates will be already formatted by .toLocaleString function
Edit:
I think I was not precise enough with the output, it should literally output MM/DD/YYYY as a string, not the actual date value
A) First of all, your desired date string conversion seems to be easy (solution below) without using any package/overhead.
B) However, if you need to process it further as a JS Date Object, things become more difficult. In this case, I like to provide some useful code snippets at least which might help you depending on your usecase is or what you are aiming for.
A) CONVERSION
Seems you need a simple swap of day and month in your date string?
In this case, you do not need to install and import the overhead of a package such as date-fns, moment or day.js.
You can use the following function:
const date1 = "16/07/2022"
const date2 = "07/16/2022"
const dateToDateFormat = (date) => {
const obj = date.split(/\//);
return `${obj[1]}/${obj[0]}/${obj[2]}`;
};
console.log("New date1:", dateToDateFormat(date1))
console.log("New date2:", dateToDateFormat(date2))
B) STRING TO DATE
Are you using your date results to simply render it as string in the frontend? In this case, the following part might be less relevant.
However, in case of processing them by using a JS Date Object, you should be aware of the following. Unfortunately, you will not be able to convert all of your desired date results/formats, here in this case date strings "16/07/2022" & "07/16/2022", with native or common JS methods to JS Date Objects in an easy way due to my understanding. Check and run the following code snippet to see what I mean:
const newDate1 = '07/16/2022'
const newDate2 = '16/07/2022'
const dateFormat1 = new Date(newDate1);
const dateFormat2 = new Date(newDate2);
console.log("dateFormat1", dateFormat1);
console.log("dateFormat2", dateFormat2);
dateFormat2 with its leading 16 results in an 'invalid date'. You can receive more details about this topic in Mozilla's documentation. Furthermore, dateFormat1 can be converted to a valid date format but the result is not correct as the day is the 15th and not 16th. This is because JS works with arrays in this case and they are zero-based. This means that JavaScript starts counting from zero when it indexes an array (... without going into further details).
CHECK VALIDITY
In general, if you need to further process a date string, here "16/07/2022" or "07/16/2022", by converting it to a JS Date Object, you can in any case check if you succeed and a simple conversion with JS methods provides a valid Date format with the following function. At least you have kind of a control over the 'invalid date' problem:
const newDate1 = '07/16/2022'
const newDate2 = '16/07/2022'
const dateFormat1 = new Date(newDate1);
const dateFormat2 = new Date(newDate2);
function isDateValidFormat(date) {
return date instanceof Date && !isNaN(date);
}
console.log("JS Date Object?", isDateValidFormat(dateFormat1));
console.log("JS Date Object?", isDateValidFormat(dateFormat2));
Now, what is the benefit? You can use this function for further processing of your date format depending on what you need it for. As I said, it will not help us too much as we still can have valid date formats but with a falsy output (15th instead of 16th).
CONVERT TO DATE OBJECT BY KNOWING THE FORMAT
The following function converts any of your provided kinds of dates ("MM/DD/YYYY" or "DD/MM/YYYY") to a valid JS Date Object and - at the same time - a correct date. However, drawback is that it assumes to know what kind of input is used; "MM/DD/YYYY" or "DD/MM/YYYY". The dilemma is, that this information is crucial. For example, JS does not know if, for example, "07/12/2022" is "MM/DD/YYYY" or "DD/MM/YYYY". It would return a wrong result.
const newDate1 = "07/16/2022"
const newDate2 = "16/07/2022"
function convertToValidDateObject(date, inputFormat) {
const obj = date.split(/\//);
const obj0 = Number(obj[0])
const obj1 = Number(obj[1])
const obj2 = obj[2]
//
// CHECK INPUT FORMAT
if (inputFormat === "MM/DD/YYYY") {
return new Date(obj2, obj0-1, obj1+1);
} else if (inputFormat === "DD/MM/YYYY") {
return new Date(obj2, obj1-1, obj0+1);
} else {
return "ERROR! Check, if your input is valid!"
}
}
console.log("newDate1:", convertToValidDateObject(newDate1, "MM/DD/YYYY"))
console.log("newDate2:", convertToValidDateObject(newDate2, "DD/MM/YYYY"))
console.log("newDate2:", convertToValidDateObject(newDate2, "MM/YYYY"))
If the wrong format is provided as a second argument, an error is provided in the console. In practise I suggest you to use a try-catch block ( I tried here, but it does not work here in this stackoverflow editor).
I wish you good luck. Hope these information can help you.

c# datetime utc string and the z when using in javascript

I am getting a date from my api and the format i get is "2019-04-17T15:04:28"
the date is UTC.
Should the date i get back have the Z at the end or doesn't it matter. Javascript date function will display incorrect if the Z is not there wont it?
Thanks #Esko thanks i think for me the confusion is that in .net core if you change the json serializer options in startup.cs by the following:
AddJsonOptions(opt =>{
opt.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
});
the tool tip in visual studio says yet it doesn't put the Z on and the documentation also doesn't show the Z (https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_DateFormatHandling.htm)
Instead i am going to try and set a different option
opt.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
If you know the timezone to be constant, you can always just append +0Z00 to your datestring. For example, if the server serializes time into CEST, turn "2019-04-17T15:04:28" into "2019-04-17T15:04:28+0100"
Parsing "2019-04-17T15:04:28" in new Date will parse it as if it's in your local timezone - the meaning of this string depends on what timezone it's parsed in.
Parsing "2019-04-17T15:04:28Z" will parse it in GMT - meaning that no matter what system parses it, it will always refer to the same time.
In other words, "2019-04-17T15:04:28" as a timestamp is ambiguous unless you know the timezone it was recorded in.
I had the similar problem and I found the answer
In my case the DateTime was with Kind Unspecified, in this case the JsonConvert hasn't enough information to add the Z in the end.
You have several options:
Convert it to UTC with ToUniversalTime method
If you're sure you're working with UTC you can add it to general JsonOptions like this:
JsonConvert.DefaultSettings = (() =>
{
return new JsonSerializerSettings
{
Converters = new List<JsonConverter>() {new IsoDateTimeConverter { DateTimeStyles= System.Globalization.DateTimeStyles.AssumeUniversal } }
};
});
if you just want to add the Kind=UTC to current Unspecified Date, you can always write isoConverter like this:
JsonSerializerSettings
{
Converters = new List<JsonConverter>() { new CustomIsoDateTimeConverter() }
};
Where the class will look like this:
public class CustomIsoDateTimeConverter : IsoDateTimeConverter
{
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (value is DateTime dateTime && dateTime.Kind == DateTimeKind.Unspecified)
value = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
base.WriteJson(writer, value, serializer);
}
}

How to convert string to date type in MarkLogic?

I am currently having some challenge in converting String data type to Date type. I used the MarkLogic JavaScript function xdmp.parseDateTime, but I am always getting the error below:
Scenario: Convert "2013-04-21" (string) to 2013-04-21 (date type)
Code:
let targetDateString = "2013-04-21";
let targetDate = new Date();
targetDate = xdmp.parseDateTime("[Y0001]-[M01]-[D01]",
xs.date(targetDate));
Error Info:
XDMP-ARGTYPE: xdmp.parseDateTime("[Y0001]-[M01]-[D01]", xs.date("2013-04-21")) -- arg2 is not of type String
Am I using the right MarkLogic function, supplying the right parameters to it?
Or is there a better way to do it?
And how do I cast a date back to a string data type?
xs.date("2013-04-21") is the xquery date constructor (ported to JS), taking a string and returning an xs:date. xs.dateTime("2013-04-21T00:00:00") would get you an xs:dateTime.
xdmp.parseDateTime can turn a string to xs:dateTime from more formats, the second term is a string: xdmp.parseDateTime("[Y0001]-[M01]-[D01]", targetDateString)
See https://docs.marklogic.com/xdmp.parseDateTime
Converting back to a string is just fn.string(yourdate)
you can directly use the constructor of date class.
var d = new Date("2013-04-21");
console.log(d);
you can even use it with different formats, Ref.

Dijit DateTextBox - setting the date in ISO/numeric format?

I'm using DateTextBox as one of many controls in my screen. I register them in one place and then batch set values to them in loop, calling set('value', val) on each of them. All controls behave correctly, only the DateTextBox won't accept the data from server.
Initially java's Date was serialized as long (ex. 1280959200000), but when I've changed to ISO format (ex. "2010-08-04T22:00:00.000+0000") it isn't accepted either. But both are accepted date formats for new Date() constructor.
On the output I get the date value in ISO format: "2013-08-04T22:00:00.000Z" so it should be also accepted on input.
What can I do with DateTextBox to make it accept values in all formats supported by JavaScript's Date object, or one of the formats that can be returned from my server?
I think the fundamental problem is that the Javascript built-in Date object only accepts certain formats, and Dojo is relying on that built-in-behavior. At work, we have a similar issue, where lots of legacy PHP code is accustomed to passing dates around in a Mysql-derived format (ex. YYYY-MM-DD HH:MM:SS)
Our current workaround is to subclass dijit/form/DateTextBox, which also lets us impose some UI improvements. When something tries to set a value which isn't already a Date object and looks like a MySQL datetime, this code re-forms it to match ISO-8601 and passes it on through.
Dojo 1.9 code for a custom version of DateTextBox:
define([
"dojo/_base/declare",
"dojo/_base/lang",
"dojo/_base/array",
"dijit/form/DateTextBox"
], function(declare, lang, array, DateTextBox){
var clazz = declare([DateTextBox], {
_mysqlStyleExp : /^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})$/,
postMixInProperties: function(){ // change value string to Date object
this.inherited(arguments);
this.constraints.datePattern = "yyyy-MM-dd"; // Affects display to user
},
_convertMysqlDateToIso: function(value){
value = lang.trim(value);
var matches = this._mysqlStyleExp.exec(value);
if(matches !== null){
// Put the "T" in the middle and add fractional seconds with UTC
// timezone
// If your MySQL dates are NOT in UTC, obviously this will screw things up!
return matches[1] + "T" + matches[2] + ".000Z";
}else{
return null;
}
},
_setValueAttr : function(/*Date|String*/ value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
/*
We want to be slightly more permissive in terms of the strings that can be set in order to support
older code... But someday it'd be nice to standardize on Date.toJSON, so warn.
*/
if(typeof(value) === "string"){
var isoDate = this._convertMysqlDateToIso(value);
if(isoDate !== null){
console.warn("Converting non-ISO date of "+value);
value = isoDate;
}
}
this.inherited(arguments);
}
});
return clazz;
});
Note that this only affects data flowing into the Dojo widget.
The docs say:
The value of this widget as a JavaScript Date object, with only
year/month/day specified.
So instead of this (which I assume you're currently doing):
new dijit.form.DateTextBox({value: "2010-08-04T22:00:00.000+0000"}, domNode);
Do this:
var myDate = new Date("2010-08-04T22:00:00.000+0000");
new dijit.form.DateTextBox({value: myDate}, domNode);

Convert Javascript Date to ASP.NET Date format

I would like to convert a Javascript date format to ASP.NET date format.
2012-09-10 12:00PM to /Date(1347442050050-0700)/
Because I'm passing it back to the server. I got the ASP.NET format from the request I did on the server, then convert it to Javascript date using moment.js:
moment("/Date(1347442050050-0700)/").format("YYYY-MM-DD hh:mmA");
Is there a way to do this?
I got what i need. If this is somehow wrong please comment.
var test = moment("2012-09-10 12:00PM").valueOf();
var test2 = moment("2012-09-10 12:00PM").format("ZZ");
var test1 = "/Date("+test+test2+")/";
alert( test1 ); // returns /Date(1347206400000+0800)/
var string = moment(test1).format("YYYY-MM-DD hh:mmA");
alert( string );​ // returns 2012-09-10 12:00PM
You can add the function to the moment prototype so that it's a little more portable.
http://jsfiddle.net/timrwood/qe8pk/
moment.fn.toASP = function () {
return '/Date(' + (+this) + this.format('ZZ') + ')';
}
If you want to send a date back to an ASP.NET ASMX web service where the RPC method receives a DateTime object, this may be helpful: https://stackoverflow.com/a/12973157/1145963

Categories