I want to compare two dates. I'm new in Angular
The two dates are in same format mm/dd/yyyy
<pre>{{CurrentDate | date:'MM/dd/yyyy' }}</pre>
<pre>{{cmp_actl_del_date }}</pre>
these three are my dates. I want to check cmp_actl_del_date > the current date.
JavaScript uses objects to store date values. When you want to compare if two dates are equal don't do this like date1 === date2. That will just tell you if the two variables reference the same object.
To check if two dates are equal use the getTime() method of the date object. Like this:
date1.getTime() == date2.getTime()
Be careful because any difference in seconds will fail to match the dates.
Now ng-if will accept AngularJS expressions.
Ensure that both variables are valid JavaScript date objects.
I recommend that you use the moment javascript library for date processing. It makes working with dates a lot easier.
Related
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
Using angularjs, dynamodb as DB here.
I have a form where user saves some data. I save my "CreateOn" date in my dynamo db as:
DateTime.UtcNow.ToString("o");
//This saves date in DB as:2018-08-21T12:58:08.7823906Z
Storing like this because dynamo db requires dates (string) to stored in ISO8601 format if you want to use between operator to search for date range.
Now I have a search filters on my page which is basically an angular calendar. When the user selects the date in the calendar( start and end date) I want to get the data back based on the selected date. Here I am using moment to pass the calendar selected date to my api call as:
moment(createdOn).toISOString()
Eg: If they select the Today's date in the calendar I pass the selected date
(Tue Aug 21 2018 00:00:00 GMT-0400 (Eastern Daylight Time)) to the above function
The result of passing this date to moment(createdOn).toISOString() is
2018-08-21T04:00:00.000Z
The search condition at dynamo db is:
conditions.Add(new ScanCondition("CreatedOn", ScanOperator.Between, startDate, endDate ));
If the user selects from the calendar the start date as "08-20-2018" (2018-08-20T04:00:00.000Z) and the end date is "08-21-2018"(2018-08-21T04:00:00.000Z), the code all the data created b/w these 2 dates.
Now the issue is if they select same start and end date then the code does not returns any data, I believe because the start and end date is "2018-08-21T04:00:00.000Z" and the time part of this is all 0000 etc.
My question is how can I convert the date from my calendar ie my end date to correctly reflect the the end time which they select. I havent used ISO8601 format before so not sure how can I do so.
Thanks
You don't need moment for this. You can zero out the time using a Date in the following way.
const date = new Date('2018-08-21T12:58:08.7823906Z')
date.setUTCHours(0)
date.setUTCMinutes(0)
date.setUTCSeconds(0)
date.setUTCMilliseconds(0)
Then you can simply use toISOString() to format the date.
date.toISOString()
// returns '2018-08-21T00:00:00.000Z'
If you don't want to zero out the time, and instead want to set some specific time, you can use a similar approach, just substitute the 0 with whatever time you want.
Some other things to note: DynamoDB doesn't require any specific formatting for dates. DynamoDB simply does a string or number comparison depending on what the field is defined as. You could store your dates in DynamoDB as integers or another string format if you feel that would be easier to work with.
Also, I'm not sure how your table is setup but make sure that your "CreateOn" field is the Range key and that you are using Query, not Scan. Using the Scan operation doesn't scale well.
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
I'm working with the Blog app and I see how to filter the Blog posts by year using the Visual Query Designer. I use the querystring value that has the year and in the ValueFilter and my properties are as follows:
Attribute: PublicationMoment
Value: [QueryString:year]-01-01 and [QueryString:year]-12-31
Operation: between
How would I get the posts from a specific month and year, if those values are passed via query string parameters. Because the months of the year have a varying number of days, I'm not sure how you would accomplish this in the Value field of the ValueFilter. Currently I'm passing the 2 digit month as the parameter.
I tried something like: [QueryString:year]-[Querystring:month]
Operation: contains
but the above operation doesn't really work because the datatype is a DateTime object.
I could do it in the razor view but I'm afraid that the paging datasource would have too many pages in it since it would be based on the larger subset of posts for the given year that was passed in the querystring parameter.
Is there any way to do this with the filter?
Basically dates are not perfectly handled yet, but there are a few ways to do it using the visual query:
Use the correct date in the query like between [QueryString:Start] and [QueryString:End] and calculate the correct dates there where you generate the links
Since your main problem with the "between" filter is actually that it would include the last day too, you could also use a two filters a >= first date and another < second date, so the first-date would be the year/month and day 1; the second one is year-month and day 1 as well
Last but not least: if you do it with razor and LINQ you shouldn't run into any performance issues - it's technically the same thing the pipeline does and it's been tested to perform well with tens of thousands of records.
i am trying to filter rows based on date as an criteria.
Using the input type date as one of the search criteria, and filtering the rows based on the attribute name is not working.
ex - <input type="date" ng-model="searchc_date" name="dtCsltDoctor">
<li ng-repeat="cs in consultations|filter:searchc_date">
Is not yielding any result. I changed the data to be of same format. No matching rows.
Fiddle for the code
The issue is still with your date formats. Because dates in JS are just a royal pain, whenever I do comparisons I usually use a library like moment.js to format the dates so I can make them match.
<pre>fake code to get past SO's requirement</pre>
This works: http://jsfiddle.net/6432E/8/