Angular Js: date field from database showing one day less - angularjs

What I have in my #Entity Person class -
#JsonFormat(pattern="yyyy-MM-dd")
private Date dob;
What I am printing on screen-
<tr ng-repeat="person in vm.persons">
<td>{{person.id}}</td>
<td>{{person.name}}</td>
<td>{{person.location}}</td>
<td>{{person.dob}}</td>
</tr>
the person.dob is showing date 1 day lesser than what exist in the database.
What could be the possible reason ?
Note:
I have dob as date type in database which is Microsoft SQL Server.
The JSON response from REST call returns timestamp which is being converted to yyyy-mm-dd format as shown above.
Example of issue:-
In database dob(s) are:-
dob
1989-05-18
1989-05-18
1970-01-01
on screen showing as below-
Person Dob
1989-05-17
1989-05-17
1969-12-31
Let me know if need more info on the issue.

I would recommend using moment js or something to get UTC date. This is a common problem between server and UI

Related

Angular Date Pipe displays wrong time?

Here is a simple line of code I am using:
<td style="text-align: center;"> {{ item.StartAt | date:"HH:MM"}}</td>
The data is fetched from database which has a DateTime column. Data is in object form so that I can convert date time to time. Without the Date pipe, the received value on the website is:
1900-01-01T10:00:00.000Z
But with the date pipe, the time received is:
14:01
As you can see there is a difference of 4 hours and 1 minute time. Can someone please tell how can I get exact time? Thank you.
Since Date Pipes have an issue of Timezone in Angular, I went to the database (SQL Server) and there I changed the Stored Procedure to fetch only Time instead of DateTime using convert(varchar, sd.StartAt, 24). Now it is working fine.
capital "M" is month , you need to have
<td style="text-align: center;"> {{ item.StartAt | date:"HH:mm"}}</td>

formatting date in angularJS table

I am creating a web app in angularJS I have a date in the following format
"response.data[0].Date = "/Date(1539887400000)/"
I can convert this date into normal MM-DD-YYYY with moment, like this
moment(response.data[0].Date).format('DD-MM-YYYY');
but while binding the data into table with ng-repeat I am not able to achieve this
I did something like this
<td>{{d.Date| date:'DD-MM-YYYY' }}</td>
but in table it still showing like /Date(1539887400000)/
what I need to do to convert /Date(1539887400000)/ into DD-MM-YYYY
Your issue is because the date filter you are using is an AngularJS defined one, and is designed to work on date objects, the number of seconds since the epoch, or specifically formatted strings (see: https://docs.angularjs.org/api/ng/filter/date). In your case, you are passing in a string "Date(1539887400000)", which doesn't adhere to any of specific formats that the date argument of the filter expects. Here are a few ways I would think about solving this:
You could initialize the date option after you have fetched the data from the database. An example would be: $scope.testDate = new Date(1539887400000);. You can then use the regular angular date filter on this object: {{testDate | date: 'dd-MM-yyyy'}}.
You can use angular-moment (link: https://github.com/urish/angular-moment), which has more features than the regular angularJS date filter. You can then use the amDateFormat and pass your string in, like so: {{d.Date | amDateFormat: 'DD-MM-YYYY'}}
You could parse out the number from the string that you currently have. Since the regular angularJS date filter can accept a number (seconds since epoch) as it's argument, it will properly format it.
See an example plunker here showing some of these in action: https://plnkr.co/edit/quCvL3GhSux8ctYQqAwA

AngularJS layered date filter for database submission

I am having trouble with date submissions to a database. When date and time are both needed, I parse the values from the database as a timestamp, which gives me the value of the date and time in milliseconds since 1970. Using the AngularJS date filter, I am able to display the value in a user-friendly way.
$filter('date')(1380292078000, 'MM/dd/yyyy hh:mm a'); => 09/27/2013 10:27 AM
But this format as a timestamp when I send the value back to the database, my code throws an error. The database wants the date to be displayed in a different format.
java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss
So I add a second filter to convert the value date representation of the millisecond count (the user might have changed the value), but I am just getting a clone of the first date representation.
$filter('date')('09/27/2013 10:27 AM', 'yyyy-mm-dd hh:mm:ss') => 09/27/2013 10:27 AM
Here is a link to the fiddle I wrote to test this. Are we not able to stack filter conversions on top of each other? I had a thought to convert the first conversion result into milliseconds to use as a source for the second conversion, but I didn't see a method to accomplish this in the date documentation. If that would be the only solution, though, I am sure I could whip something up. Because Java has mostly deprecated functions to work with dates, I feel it would be simpler to handle this on the Javascript/AngularJS side.
The date filter will only work with a JSON date string (see source).
You can get around your problem by converting to a date object:
$scope.date2 = $filter('date')(new Date($scope.date), 'yyyy-MM-dd hh:mm:ss')
See updated fiddle
However... You should be validating and formatting this input on the server side. You can't trust data coming from a javascript application to be in the correct or expected format. I'd recommend just posting back $scope.date and allowing the server to format in your database format. By doing the yyyy-MM-dd hh:mm:ss formatting on the client-side, you're tightly coupling your user interface to database implementation and that's generally considered a bad idea.

Issue in Sorting string Date field in google app engine datastore

I have given Date field in String type in Google App Engine Datastore. Because of that I am not able to sort date field in any order.
Fname = db.StringProperty()
Lname = db.StringProperty()
Date = db.db.StringProperty() #Format: DD/MM/YYYY
Now I can't change date field because I am having around 800 data records in database.
Please help me to sort this problem..
You have only a few choices.
Change the model to use db.DateProperty
Change the format of your string to YYYY-MM-DD
Sort the query results in memory with a lambda that converts date strings to real dates when doing the sort comparison.
created an additional DateProperty and duplicate the string as a DateProperty
oh and 800 records is not much to convert ;-) Do it via remote api and you won't have to deploy additional code to perform the conversion. Though you do need to update your model if you use option 1 or 4.

How to display a UK date on a HTML Form, then save it in a standard format to the database

I'm building a web app and the customer has requested that any date field that must have data entered in to it be in the UK format (DD/MM/YYYY). Currently, I have it so that all the date fields must have a date entered in the YYYY/MM/DD format because this is what the database uses.
How can I let a user enter a date in the UK format, but then flip it around to then save it correctly to the database?
In beforeSave callback check if a date field is provided and if so, use DateTime::createFromFormat to create a DateTime object from your display format. And then change it to sql format via DateTime::format()
if (isset($this->data[$this->alias]['date_field'])) {
$this->data[$this->alias]['date_field'] = DateTime::createFromFormat('d/m/Y', $this->data[$this->alias]['date_field'])->format('Y/m/d');
}

Resources