AngularJS deserialize $http JSON dates i18n - json

I have a problem and I don't quite know how to deal with it. It must be a common problem, but I didn't find a good answer yet.
I'm coding a MEAN stack app, using Mongoose, and the problem I have is with the dates format when changing the language in my app.
The scenario is: The user can insert some data that has a Date field, for example their birthday. That field is then sent to mongoose in a specific format (yyyy-MM-dd) and it gets stored correctly in a Date field in mongoose. When the user searches for the data stored, I have an $http get request that get back the data in JSON format. Right before the data is send from the server, I have this in the JSON:
{datetime: '2015-11-13T20:00:00.000Z'}
And send it from nodejs like this:
return res.status(201).send(obj);
And when the Angular gets it, depending on the language I'm using in that moment, it resolves to a Date or an Invalid date. If the language I'm using is English, the resulting JSON has a valid Date in that field. If I change it to Spanish for example, then it results into an Invalid date field.
The code in Angular is this:
$http.get(url).then(function (result) {
//here the result.data may have an Invalid Date field
deferred.resolve(result.data);
}, function (error) {
deferred.reject(error);
});
I set the language in my app with the help of angular-translate, and I use angular-i18n and momentjs also.
I've read on this and here https://docs.angularjs.org/api/ng/service/$http it say that
If JSON response is detected, deserialize it using a JSON parser.
I understand that the default parser detects the Spanish i18n and tries to parse the date as yyyy-dd-MM, and obviously gets an Invalid Date.
The question here is: How can I explicitly set all the dates parsed in Angular, to follow a specific format, like yyyy-MM-dd, regardless of the i18n used? I've seen this solution already, but doesn't work for me: http://aboutcode.net/2013/07/27/json-date-parsing-angularjs.html
Is there a better way to handle all this? Maybe store the dates in Mongo not as Date fields, but like Strings, using the JS getTime() function?
This last method means that I would have to change all the dates in my app. It's a lot more work, but the result will be more standard, right?
Thank you!

Related

osisoft updating values ​with python

I want to get the data from Excel and update the values ​​of the data between certain dates in the osisoft system. But I don't know how to code AfTimeRange.
I get the error "value cannot be converted to OSIsoft.AF.Time.AFTime".
enter image description here
Good news: You've constructed your [AFTimeRange] correctly!
Notice that the error being thrown is not by the [AFTimeRange] constructor, but from something else.
The issue is that you are trying to set the 'Timestamp' attribute [OSIsoft.AF.Time.AFTime] to a value of type [OSIsoft.AF.Time.AFTimeRange], and so it's failing. A PI event has a single timestamp, not a time range.
I'm not familiar with Python, but it should work if you input the value as an AFTime object using its string constructor, assuming you're intending to use yesterday midnight as your timestamp:
val.timestamp = AFTime("y")
See the documentation on AFTime for more detail.

Weird date format from mysql in vuejs

Time in mysql is stored like this
2022-04-25 11:03:20
but when it is showed on client with vuejs i am getting this as response
2022-04-25T09:03:20.000Z
How do i show it as it is shown in db?
The date you're getting is in ISO-8601 format. You'll want to continue to store it like that so you've got the timezone (as denoted by the suffix 'Z'). In your frontend, it's easy to convert the date into any format you like.
For example, based on your example, you can use:
const dateFromDb = '2022-04-25T09:03:20.000Z';
const dateForUi = new Date(dateFromDb).toLocaleString('nl-NL');
console.log(dateForUi); // 25-4-2022 10:03:20
You can also convert any date object back into ISO format, using .toISOString().
There's also many other date formatting methods (outlined in the Date() docs), or for more advanced date / time operations there are libraries like moment.js

How to deserialize asp.net datetime as JSON datetime with client's timezone

I'm struggling with datetimes a bit. I'm using asp.net mvc api controllers, a microsoft sql server and AngularJS.
On some button click I'm sending a JSON formatted date to an api-controller. When I post 2015-11-31 00:00 and I look in Fiddler to see what's really posted, I see that the date is formatted as such: 2015-11-30T23:00:00.000Z. (2015-11-31 - 1 hour UTC+01:00 Amsterdam, Berlin, Ber....) This is perfect because there might be a difference between the timezone the sql server might be in and the client. (Or is it?)
The problem is though: When I get the date back from the sql server it doesn't take the client's time zone into account. When I read the DateTime object from the sql server and I return it JSON formatted, the date that's being displayed is: 2015-11-30T23:00:00.000Z. I want it to add 1 hour to be in the timezone where the client is.
My question is: What do I do to get it to keep the timezone in to account while deserializing the JSON string that comes back from my api-controller?
TIA!
Problem turns out to be that when the object is being deserialized, the date property is not of type DateTime. It is of type string. Simply converting it to date by using new Date("2015-11-30T23:00:00.000Z") will do the trick.
I made filter for it:
.filter('from_gmt_to_local_date', [function () {
return function (text) {
return new Date(text);
};
}])
Usage:
{{contract.StartDate | from_gmt_to_local_date | date:'dd-MM-yyyy'}}
Hope this helps anybody.

Typescript mapping Json to interface parsing date

I'm mapping my JSON responses from the Server side code into an Interface, in this way;
objectFromJson: IMyObject = <IMyObject>jsonData;
The problem is, that the Json contains a Date, is there a way to automatic cast the date to an real Typescript Date without something like this:
new Date(parseInt(incident["CreatedOn"].substr(6)));
that the Json contains a Date, is there a way to automatic cast the date to an real Typescript Date without something like this
Your json seems to create date as a number. BAD IDEA. Reason:
how about dates before 1970
it is unreliable based on time zones
Prefer you return dates as strings. More API recommendations : https://github.com/interagent/http-api-design
If all you have is it returned as number than what you have is okay. Else if you have dates in a JavaScript recommended format e.g. 2012-01-01T12:00:00Z you would do var date = new Date('2012-01-01T12:00:00Z')

Spring roo not recognizing date format before saving json

I'm using Spring Roo on top of MySQL. I pull dates out via the Roo-generated JSON methods, make changes to that data in a browser form, and save it back via the Roo-generated JSON methods.
The date format I'm getting out is yyyy-MM-dd, standard MySQL date format. I'm using a calendaring widget on the browser to ensure the date I'm submitting is the same format.
Unfortunately my data doesn't go right through the ...FromJson() method, failing with the error:
Parsing date 2007-12-12 was not recognized as a date format
I presume that the problem is that it's coming out as a string, but JPA feels like it needs to generate a Date object to update.
I'll happily show my code about this, but it's nothing Roo didn't build for me.
It occurs to me that there's something it's referring to when it says "recognized as a date format". Is there somewhere I can change what date formats it knows?
EDIT: With #nowaq's help, here's the ultimate answer:
public static Lease fromJsonToLease(String json) {
return new JSONDeserializer<Lease>()
.use(null, Lease.class)
.use(Date.class, new DateFormatter("yyyy-MM-dd"))
.deserialize(json);
}
That way JSONDeserializer knows what class it's dealing with AND builds a formatter for all the dates in that class. Wicked!
Your question is very related to this one: Spring-roo REST JSON controllers mangle date fields Take a look and make sure you're using correct DateTrasformer(s) with your JSON deserializers. E.g.
new JSONDeserializer()
.use(Date.class, new DateTransformer("yyyy-MM-dd") )
.deserialize( people );