<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>REST &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/rest/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 22 May 2021 17:39:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Angular calendar datepicker creates incorrect date one day behind the selection</title>
		<link>https://codepills.com/angular-calendar-datepicker-creates-incorrect-date-one-day-behind-the-selection/</link>
					<comments>https://codepills.com/angular-calendar-datepicker-creates-incorrect-date-one-day-behind-the-selection/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Sat, 22 Jun 2019 12:51:03 +0000</pubDate>
				<category><![CDATA[Bug hunt]]></category>
		<category><![CDATA[Angular]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Spring]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1015</guid>

					<description><![CDATA[In this article, we will be looking at Angular datepicker and transferring the correct date through the REST endpoint. <a href="https://codepills.com/angular-calendar-datepicker-creates-incorrect-date-one-day-behind-the-selection/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>I was working on the bug fix task, which aimed to solve an extraordinary issue at first sight. In the monolithic Angular-Spring application, I choose a specific date at the front end with an Angular calendar datepicker. However, the database suddenly saved one day before the initially selected date.</p>
<p><span id="more-1015"></span></p>
<p>I started investigating the cause of the issue. First, I examined with Spring backend REST endpoint call with the breakpoint. The entity with the date was already coming incorrect from the Angular frontend. The backend endpoint on entry showed incorrect data, so the error was automatically not in the backend code.</p>
<p>At the frontend call for the backend REST endpoint, I also used <code>JSON.stringify(object)</code> to check if the produced entity which was leaving the frontend to the backend was correct. What I found was that it was also already incorrect.</p>
<p>Consequently, the issue happened somewhere between the user browser and Angular call for the backend. In this way, I also eliminated the possibility that the problem was somewhere in the Spring JSON conversion library (I used Jackson).</p>
<p>In the end, I figured it out. The issue was in class, which was migrating datepicker form data into the object. While the Angular calendar datepicker <u>collect the date from the browser also with local time zone</u> as a string, data to JSON ware pushed as a JavaScript <code>Date</code> class without a time zone. So if you, for example, pickup up any date, it was selected as the date for midnight with the timezone offset and transferred from string to <code>Date</code> as the day before due the offset counted as date subtraction.</p>
<p>I was working in Central Europe. In the case of Central Europe (CET), where I was working, the browser records +1 hour from GMT in datepicker based on local PC time settings. So if I pick up the date, hours and minutes are selected for midnight (00:00). However, <code>Date</code> class takes the datepicker string and subtract the negative offset from midnight and set a selected date for a day before.</p>
<p>This is the Angular&#8217;s datepicker component HTML code from the component form:</p>
<pre><code class="language-typescript">&lt;div class="ui-g-6"&gt;
    &lt;div&gt;
        &lt;label for="startDate"&gt;Start Date&lt;/label&gt;
    &lt;/div&gt;
    &lt;vrm-form-field [control]="editForm.controls['startDate']"&gt;
        &lt;p-calendar id="startDate"
            [formControlName]="'startDate'"
            dateFormat="yy-mm-dd"
            yearRange="1900:2100"&gt;
        &lt;/p-calendar&gt;
    &lt;/vrm-form-field&gt;
&lt;/div&gt;</code></pre>
<p>Following static function created an object from form datepicker which was pushed to REST endpoint before the fix:</p>
<pre><code class="language-typescript">static mapFromFormData(form: any) {
    return new ImportantDate(form.startDate);
}</code></pre>
<p>To fix the problem, I created a workaround. I made a Utility class with <code>moment library</code>, which takes a datepicker string and select only required date information.</p>
<pre><code class="language-typescript">export class Utils {
  public static formatDate(date: any): string {
    if (date === undefined || date === null) {
      return undefined;
    } else {
      return moment(date).format('YYYY-MM-DD');
    }
  }
}</code></pre>
<p>You can see the implementation of the static Utils method, which fixed the date.</p>
<pre><code class="language-typescript">static mapFromFormData(form: any) {
    return new ImportantDate(
        Utils.formatDate(form.startDate)
    );
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/angular-calendar-datepicker-creates-incorrect-date-one-day-behind-the-selection/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
