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.
Related
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
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')
Developing a component for Joomla v2.5, I'm using a table with a mysql timestamp column.
One of the component's settings is the "Timezone". I don't want to use server timezone, as code will run on different servers/timezones and I want to be indepedent. So the idea is to store timestamps in mysql, and display the correct date/time according to the component's parameter. The main drawback is the timezone that mysql server uses, that make the whole situation complicated. So, is there a way to store current timestamp in an universal format in MySQL and display it in the correct way?
The ultimate goal is for the component to be able to display the correct date/time based on the component's parameter, eg. user changes the parameter on the fly, no modification on the database take place, only on the "View"
In order to display the date in the correct timezone I use this:
JHtml::date($date_from_mysql , 'd/m/Y H:i:s', $my_component_timezone_parameter)
Please share your thoughts.
try
jimport ('joomla.utilities.date');
$date = new JDate($mydate);
$curdate = $date->toFormat('%Y-%m-%d %H:%M:%S');
for timezone settings
try http://docs.joomla.org/JDate::setTimezone/1.6
http://www.webamoeba.co.uk/site/index.php/articles-joomla-date-time
or try to override the store method in your table class:
public function store($updateNulls = false)
{
// get date
$date = JFactory::getDate();
// set variable for timestamp
$this->myDate = $date->toMySQL();
return parent::store($updateNulls);
}
I have fields in a Mysql database typed datetime.
I store, for example, a payment's date with next Java code:
payment.setCreatedOn(new Date(System.currentTimeMillis()));
In my view layer I use fmt:formatDate to format dates:
<fmt:formatDate value="${payment.createdOn}" pattern="EEE, dd MMM yyyy HH:mm:ss"/>
My server is in London and my application's users are in Vienna. The time showing is delayed probably because of different time zones. I can use a timeZone Parameter in fmt:formatDate.
timeZone: Time zone in which to
represent the formatted time.
After searching in Google, I think the value Europe/Vienna is valid for timeZone parameter.
Does anyone knows if there is list anywhere of the valid timeZone strings?
Sergio - I'm sure you've long since found it anyway ...
Here's the homepage for the Olson database or 'zoneinfo' which is the definitive source of TZ info.
And here's a nice wiki for browsing zones.
Here is a list for the IBM JDK, a little dated but has full list:
http://publib.boulder.ibm.com/infocenter/wsdoc400/v6r0/index.jsp?topic=/com.ibm.websphere.iseries.doc/info/ae/ae/adrtzval.htm
You need to use the international time (UTC/Zulu) to add the following schedule client time use, for example "GMT+1". See this example.
Put this parameter as argument in your server to set UTC time use, in this case is for tomcat:
-Duser.timezone="UTC"
/* Java */
#RequestMapping(value = "/web", method = { RequestMethod.POST, RequestMethod.GET })
public String web(Model model, HttpSession session, Locale locale) {
Date today = new Date();
model.addAttribute("currentTime", today);
model.addAttribute("timezone", "GMT+1");
return "web";
}
To show date choose your pattern you want (properties)
/* JSP web */
<fmt:timeZone value="${timezone}">
<spring:message code="date_format_dateMin" var="pattern"/>
<fmt:formatDate value="${currentTime}" timeZone="${timezone}" pattern="${pattern}" var="searchFormated" />
<span class="innerLabel">${searchFormated}</span>
</fmt:timeZone>
/* Properties */
date_format_dateMin=yyyy/MM/dd HH:mm
date_format=yyyy/MM/dd HH:mm:ss
date_format2=yyyy/MM/dd
date_format3_js=yy/mm/dd
date_format4_time=HH:mm
date_format4=dd/MM/yyyy HH:mm:ss