Date time not formating in AngularJs 1.6 - angularjs

I've got this format of date coming from an api:
"dateTime": "2018-02-19 00:00:00.000-08:00"
I just want the days, month and year in the format: dd/MM/yyyy
I'm using this in the view:
<td>{{dado.dateTime | date:'dd/MM/yyyy'}}</td>
But it's shows the date extacly as it comes from the API. Without any formatting
OBS: Angular 1.6

You are missing the T in your original DateTime. For ISO8601 DateTime it should be "2018-02-19T00:00:00.000-08:00" as the value. That is what can be translated by the angular date filter as a DateTime, currently it can only be interpreted as a string.
From the documentation on the input:
Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.
If you do not have any control over the server/source then you have a couple of options:
Change the value of the date time string in the factory/service when you receive the data so it is ISO8601 compliant. This can be as simple as using a regex or string concatenation as long as the T character is added between the date and time. In this specific case you could take the first 10 characters as you probably want to completely ignore the time portion but that would not work where you do want to show or keep time as a part of the datetime.
Create your own filter that then calls through to the built in date filter once you format the input correctly.

Related

Style each section of a date after it been formatted with the correct date format

My problem is styling a date object properly. I would like to style the day month year and sometimes day of the week. If the date object is static it would be simple. The date format is passed in and usually takes in the account of localization so I don't know what the format will look like. I could be given mm/dd/yyyy or mm/dd/yyyy (Simple case). Currently, we just pass it through angular date filter and it produces the correct date, but I can't style the month to be bold and blue for example.
Is there an easy way to parse a date with the format of dd/mm/yyyy and gives me:"12/12/1988"
Angular ships with a Date filter.
<p>{{'1491941202157' | date:'dd/MM/yyyy'}}</p> => 11/04/2017
Pair that up with a directive using the $filter in the controller, or simply split the string on / and wrap in style-able nodes like <span>, for example.
More here: https://docs.angularjs.org/api/ng/filter/date

SQLite: Extract time from string using strftime

I have database column with time (string is used as data type of the column) in this format '30.09.2014 09:03:01'. I'd like to get timestamp from that column using:
SELECT id, strftime('%d.%m.%Y %H:%M:%S', date) AS time FROM table;
But I get empty column with time. I think there is a problem with dots as a separators. Can anyone help me with that?
The strftime format string is used to specify the output format for printing a date that is supplied in one of the standard formats. The input formats accepted are one of the following:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
You may need to do some string manipulation to reformat the date strings into a format that sqlite can interpret. Often it is convenient to reformat the date when inserting the data initially.
Reference sqlite documentation.

Angular js Date Formatting

I want to change the date format from 10/07/2015(mm/dd/yyyy) to oct 7 ,2015 in angular js
i'm using {{'10/07/2015' | date:'longDate'}} and is giving me the same date '10/07/2015' as output
can any one help me with this?
As per the documentation, you have to format your date in a specific way for the date filter to work properly:
Documentation:
Date to format either as Date object, milliseconds (string or number) or various >ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter >versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone >is specified in the string input, the time is considered to be in the local >timezone.
Example ISO formatted:
<div>{{ "2015-10-07" | date:'longDate' }}</div>
Fiddle

Angular format date not rendering

I'm trying to use Angular's date/time formatting with this string
2015-01-20 16:49:07+00:00
{{ myDate | date : 'medium' }}
However, it doesn't seem to be working? Is the original string messed up?
According to the angular date filter docs:
Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.
You'll notice that for an ISO 8601 date, you need a T before the time:
"2015-01-20T16:49:07+00:00"
http://jsfiddle.net/p84zjy6t/
The correct usage is:
{{ date_expression | date : format }}
To check all format options check the docs

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.

Resources