Total number of days in angular? - angularjs

I'm using angular datefilter to output a countdown. I do it like this:
<p class="clock">{{timeleft| date:'dd'}}<span>:</span>{{timeleft | date:'HH'}}<span>:</span>{{timeleft | date:'mm'}}<span>:</span>{{timeleft | date:'ss'}}</p>
$scope.timeleft contains a value that is calculated from taking launch-date minus current date.
Currently, there is more than one month left before the countdown reaches 0. I would like to show the number of days in total, that is, more than the number of days in the current month.

This is more a question about calculating date differences, than something to do with angular. I have created a simple fiddle for you here: http://jsfiddle.net/IgorMinar/ADukg/
Basically you can calculate a date difference between two dates like this:
var dstring = '2014-03-09'; // date to check against the current date
var oneDay = 24*60*60*1000;
var diff = Math.floor(( Date.parse(dstring) - new Date() ) / oneDay);
Then just plug the value into angular any way you like.

Related

Display rows with current date as top result using react-table

I'm trying to create a table using react-table that would display data that matches today's date as the top result. For instance, today is 18 August 2021, so I would like any data entry that matches 08/18/2021 (mm/dd/yyyy format) to be displayed as the topmost entry in my table.
My current table looks like this
To give an example, I have already sorted my table in chronological order (from earliest to latest in mm/dd/yyyy format). However, I want any entry/entries in the table above with today's date (eg. Date of Operation: 08/18/2021) to be displayed at the top of the table. Thereafter, entries that do not match today's date will continue to be sorted in chronological order.
Can someone please let me know how this can be done? Thank you.
Without getting into the code details, you should be able to do this by supplying a custom sort function in the column definition API docs. We don't do this specific sort but we do have multiple custom sort functions, and I got the most help in writing them by reading the sortTypes.js file from the library source.
Here's a simple date sort function. You basically get two rows and a columnId and return -1, 0, or 1.
const prevDate = new Date(prev.original[columnId])
const prevIsDate = ...
const currDate = new Date(curr.original[columnId])
const currIsDate = ...
if (prevIsDate && currIsDate) {
return prevDate > currDate ? 1 : -1
} else {
if (!prevIsDate) {
return !currIsDate ? 0 : 1
} else {
return 1
}
}
}

Date comparison validation in angular

I have 2 dates: 1.start and 2.end
format is like this.
12/4/2017 console.log(startDate);
12/20/2017 console.log(endDate);
I am writing a validation to check if the end date is bigger than start date throw error,but it is not working.
this is what i have tried:
var startDate = new Date(this.formB['startDateVal']).toLocaleDateString();
var endDate=new Date(this.formB['dueDateVal']).toLocaleDateString();
this is my condition:
if(endDate<startDate){
this.bucketMsgClass='fielderror';
this.bucketSuccessMsg = 'End Date is must lower than Start Date.';
}
where am i doing wrong.?
I've always just subtracted one of the dates from the other. If the result is negative, then Date 1 is before Date 2.
var d1 = new Date("12/12/2017");
var d2 = new Date("12/13/2017");
console.log(d1 - d2) // -86400000 (exactly 1 day in milliseconds)
So
if (d1 - d2 < 0) {
// d1 is smaller
}
Going through this link that explains comparing dates in javascript would probably help you understand the problem and solve it.
Compare two dates with JavaScript

How to dynamically get the start and end date of the week

I'm using ionic framework.Is there a way to dynamically get the start and end date of the week when user selects a random date.
Based on this answer I've made a configuration based on your needs. The code is in vanilla js but you can easily translate it to angular code, since functionality is the same.
The function to find the first and last day of the week, based on user selected date:
function getFirstLastDayOfWeek(userDate) {
let result = {};
let curr = new Date(userDate); // get current date
let first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
let last = first + 6; // last day is the first day + 6
result = {
firstDay:new Date(curr.setDate(first)).toUTCString(),
lastDay:new Date(curr.setDate(last)).toUTCString()
};
return result;
};
And here's a working fiddle.
var days=new Date().getWeek();
console.log(days[0].toLocaleDateString() + ' to '+ days[1].toLocaleDateString())
Dynamic date
var days=new Date("10/01/2017").getWeek();
console.log(days[0].toLocaleDateString() + ' to '+ days[1].toLocaleDateString())
Please mention your ionic version.
Using moment.js would solve your problem easily.
Lets say you have a random date with you and wants to find start of week.
moment(<randomDate>).startOf('isoWeek');
End of the week
moment(<randomDate>).endOf('isoWeek');

Why using Angular Moment returns all january in ng-repeat?

I am using
<div ng-repeat="usage in previousUsage"
<p>{{usage.Month | amDateFormat : 'MMMM' }}</p>
</div>
usage.Month data are numbers 1-12
The data returns January
amDateFormat expects to get a date as its value. Not necessarily a timestamp, but any kind of object that moment.js will be able to create a moment object from (Date object, string, timestamp in milliseconds, etc.).
When you use a number between 1-12 as your input, moment will see it (as #pavel-horal commented) as the amount of milliseconds that had passed since the beginning of 1970-01-01. Then you use the filter to display the month, and you'll get January.
Thanks guys,
I just did
<div ng-repeat="usage in previousUsage"
<p>{{dateFilter(usage.Month) | amDateFormat : 'MMMM' }}</p>
</div>
$scope.dateFilter = function(month) {
var objDate = new Date(month);
return objDate;
};
Something along these lines - it returned back as January, February, March, etc...

find the Day of week of a particular date

i have an object which has 2 dates startdate_c and enddate_c .
i need to find a way to find the days of week these dates fall in
For example
startdate = 1 jun 2012 and enddate = 3 jun2012
I need to know which days of the week the days between these dates fall in.
In this example
Mon = false, tue = false, wed = false, thu=false, fri=true,sat=true,sun=true
I want to use this in a Vf page to render the somefields based on the boolean value.
Any pointers would be of great help.
Date has a method called toStartOfWeek which you could leverage, assuming your two dates do lie within the same week you could simply do something like this:
date weekStart = startdate.toStartOfWeek();
list<boolean> days = new list<boolean>();
for(integer i = 0; i < 7; i++)
{
days.add(weekStart.addDays(i) >= startdate && weekStart.addDays(i) <= enddate);
}
A little bit crude, but it'll give you an array of 7 boolean values. For longer/unknown ranges you could use a date cursor and increment that instead of an integer here, but this should get you started. Note, I've not tested this code ;)

Resources