We are using AngularJS to set filters. Basically we have a start date and end date and frequency. When the frequency is set to week, we simply want to add 1 week to the start date, when the frequency is set to daily, we want to add 1 day to the start date.
Basically something like :-
var date = new date();
date.addDays(2);
date.addMonths(2);
date.addYears(2);
That way angular will get a regular JavaScript Date object instead of a moment instance and it's change detection will work correctly. To advance the year by 1. Use .setDate () function to increment the days and instead of +1 day and add +365 days it will give you date after 1 year.
All types of datetime values displays the date in ‘MMM d, y’ format which is default Angular date format ‘mediumDate’. To change the datetime format in angular we have to pass date time format parameter to the angular pipe as shown below. { { date_value | date :'short'}} // 6/15/19, 5:24 PM.
JavaScript provides the Date object which is used for manipulating date and time. In this tutorial, you will learn an easy way of adding days to Javascript Date with setDate () and getDate () inbuilt functions which are used to set and get the day of the month of the Date object. Here is a utility function which creates a Date copy:
The format ‘short’ is one of the predefined date formats in angular which converts our date value to ’M/d/yy, h:mm a’ format. List of all predefined date formats in Angular. Date Format. Angular datepipe code. Result.
I would consider using moment.js for all of your JS date related needs then you can do:
var date = moment();
date.add(2, 'days');
date.add(2, 'months');
date.add(2, 'years');
// or all of the above with:
date.add({years: 2, months: 2, days: 2});
And if you need a regular JS date object at the end, check out this post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With