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
Related
DTO- XDto: private XMLGregorianCalendar date; Set to say 2017-03-22T14:25:30+01:00
REST- application/type-JSON, sends above dto as data for say a GET request
Output value while testing with Google's ARC- shows something like 1409175049 instead of something like 2017-03-22T14:25:30+01:00
Any idea how to send date as it is rather than json changing it to its own format?
Looks like it’s just sending the date as UTC Epoch. Convert that back to whatever format you want using the Date object.
How can I change JSON date format to UTC automatically?
now when I get data from server included dates, it returns as local zone date. I know I can change dates in the server with this code to UTC:
var date = DateTime.Now.ToUniversal();
but I need to make it auto.
By default, Json.NET preserves the time zone. You can override this by setting the DateTimeZoneHandling property:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
Call this from your Application_Start method, defined in the Global.asax.
You can find more information is here.
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!
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.
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')