Date Format match between client and server - ajax

Am posting an object from client to server via AJAX in ASP Net which consist of date Property too.MY Machine's time zone uses the date format MM/dd/yyyy in string whereas my server time zone uses the format dd/MM/yyyy.Am still wondering that how do the conversion takes place properly and it works as expected result which gets saved in the database.
Say for example am posting a value from client 02/01/2016(MM/dd/yyyy) in string which gets changed properly in server to 01/02/2016(dd/MM/yyyy). How do they know that the incoming value was in (MM/dd/yyyy) and then parses to (dd/MM/yyyy).
Let me know if i was wrong/misunderstood.

Related

Changing format of date without using to_char - Oracle

I have to get the max payment date on an invoice and I am having trouble with the date format. I do not need the max in this formula as I am using the format in a reporting tool that is pulling the max from what it finds for me.
Using "to_char({datefield},'mm/dd/yyyy')" works for displaying that date the way we would like BUT when you use summary function MAX it does not pull the correct date because it is looking at a string and not a date (it will think 12/3/21 is larger than 3/2/22).
Another thing I have tried is trunc - "trunc({datefield})" which gives us the correct max date but it changes the formatting. For example if the date prior to the formula being applied is "8/12/21 12:00:00:000" the trunc formula will display it as 12-08-21 which is horribly wrong.
Long story short is I need a way to change a date/time to date with the format of 'mmmm/dd/yyyy' WITHOUT converting it to a string with something like to_char. Thank you!!!!
A DATE is a binary data type consisting of 7 bytes representing: century, year-of-century, month, day, hour, minute and second. It ALWAYS has all of those components and it is NEVER stored with any (human-readable) format.
What you are seeing when a date is displayed is the client application you are using to access the database making a decision to be helpful to you, the user, and display the binary DATE provided by the database in a human-readable format.
If you want to change how the DATE is displayed then you either need to:
Change the settings on the client application that controls how it formats dates when it displays them to you; or
Change the data-type so that it is no longer a DATE (which does not have a format) to a data type where the values of the date can be formatted (such as a string). You can do this using TO_CHAR.
If you want to find the maximum then do it BEFORE applying the formatting:
SELECT TO_CHAR(MAX({datefield}),'mm/dd/yyyy')
FROM your_table;

Control UTC time in server side Dynamicscrm

I saved a record in 17:16:15, I run a job which gets the ModifiedOn field of my record and I got- 15:16:15, my GMT is +2, I want to know how to fix that gap that my result will turn out like it should be - 17:16:15. I can't select it from DB I need a solution in server side (c# I mean) what can U do in that case?
DateTimes are always saved in UTC in the database. *
You need to dynamically convert from UTC into your local time zone. In C#, you can do this with the .ToLocalTime() method as long as your code is running in the correct time zone. You can also find your local time in the FormattedValues collection of the response, which uses your Dynamics timezone user settings . But the raw datetime value in the database will always be in UTC.
* The only exception to this is if the DateTime field is set to “TimeZone Independent” in the attribute type settings. But be careful: once you set this option you can’t change it for that field again.

Spring Boot - Jackson - datetime timezone problems

I am having trouble dealing with timezones across my projects.
First of all, I have two use cases:
1) A user input a DateTime with datepicker in HTML and we want to save it without timezone in the database. example input: 10.04.2018 18:00 --> we want to save exactly this date and also want to retrieve exactly this date, no matter what the timezone is on the client side. (we can think about this like a never changing date string)
2) The more common, second use case is, we enter a DateTime with datepicker in HTML and want to save the date with timezone in the database. For example:
The Clients Timezone is Europe/Istanbul and he inputs a DateTime: 10.04.2018 18:00. Now we want to save this date as Europe/Berlin in Database which would be 10.04.2017 17:00. When we retrieve the date we want to convert the date to the timezone of the client. In this example, if a client with timezone Europe/Berlin wants to retrieve the database date 10.04.2017 17:00 we would exactly retrieve: 10.04.2017 17:00. But if a client with timezone Europe/Istanbul wants to retrieve the same value 10.04.2017 17:00 we would retrieve: 10.04.2018 18:00
Now I tried to achieve this behavior by settings the webapp timezone to Europe/Berlin and parse the dates for each use case with the Moment.js library on client site. Therefore, I always expect to retrieve a Date in the timezone Europe/Berlin from my web service and then either convert to the local time zone or let the date as it is.
I am using Spring Boot with following version:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
In my application.properties file i set those values:
spring.jpa.properties.hibernate.jdbc.time_zone=Europe/Berlin
spring.jackson.deserialization.adjust-dates-to-context-time-zone=false
spring.jackson.time-zone=Europe/Berlin
MyApplication:
#PostConstruct
void started() {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
}
My attributes in my model class look like this:
#Column(name = "timeFrom")
#Temporal(TemporalType.TIMESTAMP)
private Date timeFrom;
#Column(name = "timeTo")
#Temporal(TemporalType.TIMESTAMP)
private Date timeTo;
For the first use case, where I want a date which is not parsed to the local time zone i use this code to retrieve my date in javascript: (because I know that my date in database is Europe/Berlin, I convert it to this time zone in order to retrieve the exact same date as it is in the database)
var countHours = moment(timeTracking["countHours"]).tz('Europe/Berlin').format('HH:mm');
For the second use case, where i want to convert my Europe/Berlin date from the database to the local client time zone i use this code:
var timeFrom = moment(timeTracking["timeFrom"]).format('YYYY-MM-DD HH:mm');
When i pass a date from javascript via JSON and ajax to my backend i use this:
moment($("#timeRecord-timeFrom").val()).valueOf()
On serverside i receive a timestamp in milliseconds and i parse it like that:
Date dateFrom = new Date(node.get("startTime").asLong());
Now the weird part: everything I posted above is working perfectly fine on my localhost (local machine). However, as soon as I push my war file to the live system (Jelastic Spring Boot Server) I have an offset of 2 hours in my date.
The strangest part is when I commit my date via ajax and retrieve the persisted object afterward everything is fine and I have no offset. But when I do a full page reload, I immediately receive the date with 2 hours offset. However, the date is still correct in the database.
As this is only happening on the live system and not on the localhost I have no clue how to fix this.
What am I doing wrong? Would it be better to switch to JodaTime or Java8 LocalDateTime? I already tried it but ran into several build errors so stick with java.util.Date.
A best practice tutorial for my use cases would be also appreciated.

400 - Bad Request while saving Date Only field in CRM using Web API

I am using Microsoft Dynamics 365 Web API approach to interacting with CRM data.
Here I want to update the field with type: DATE and TIME.
Date value I am passing in the request body is as below:
"packfirstday":"1-15-2018"
Except for above, I have also tried with DateTime and with different date formats.
e.g.
mm-dd-yyyy
m-dd-yyyy
mm/dd/yyyy
yyyy/mm/dd
yyyy-mm-dd
PS: I try to post without date field it is saving details successfully.
The problem is not with the code, the simple misunderstanding.
There are 2 components namely Behavior & Format. You have Format set as 'Date only' not the Behavior. Behavior decides the Database terms whereas Format is used for displaying datepicker controls in Form.
So when you set the field in web api with only date part - the CRM database expects time part also.
Either set behavior also as date only, so this will work:
"packfirstday":"2018-01-15" //YYYY-mm-dd format
Or change your code to pass time part also:
"packfirstday":"2018-01-15T11:10:00.000Z" //UTC offset
Since user local behavior still expects the time part.

UTC DateTime problems

I currently store all dateTimes in the DB as UTC dates. Each users time zone offset is also stored in the DB. When I retrieve a Date it is converted back to their local date using this offset.
The problem occurs when I retrieve a date using an ajax call. The date (which is already converted using the offset) is, I think, returned as a Java Date object. The browser then decides to mess with my Date adding the clients computers time zone offset to the Date object. This is causing dates to be a day ahead of what they should be if the time component is more than 11.59am.
The only solution I can come up with is to pass them as strings in which case this of course wouldn't happen. This is a laaaast resort for me though and I would love to find a better solution or workaround for this problem.
Your browser is not messing with the dates given that browsers don't have a native date transfer variable. You have something else that is doing that. How are you sending your dates in ajax? Json? Json will only send numbers or strings. XML will only send strings.
Something is converting your sent date into a javascript date object, find out what it is.

Resources