Date comparison validation in angular - angularjs

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

Related

React - read dates from excel file

I am having a weird issue when I was reading dates from excel files in a react application. When I read it, it comes out as some form of float but what's weird is that it only does this for 1th to 12th date of the month from 13th till 31th is fine.
What I mean is that a date like 01-01-81 gets converted to 29587.00013888889
but 13-01-81 remains in its original.
I found this one solution from here How to read date from an excel file in JavaScript. But it does not give back the original value.
Really appreciate any kind of help. Cheers.
The problem was that excel has its own way for dates and we need to convert it, into the format we need.
This is the method I used
const ExcelDateToJSDate = (date) => {
let converted_date = new Date(Math.round((date - 25569) * 864e5));
converted_date = String(converted_date).slice(4, 15)
date = converted_date.split(" ")
let day = date[1];
let month = date[0];
month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(month) / 3 + 1
if (month.toString().length <= 1)
month = '0' + month
let year = date[2];
return String(day + '-' + month + '-' + year.slice(2, 4))
}
You can simply pass the excel date and change the function according to the date format you need.

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');

Epoch 0 to Date conversion using momnet.js

I need to convert my epoch date to a Date object. I did this using the following code.
let abc = moment(maintenance.actualEndDate * 1000).format('DD/MMM/YYYY hh:mm');
but there is a high chance to 0 as the value for 'maintenance.actualEndDate'.
in this case the transalated date is showing the value as '01/01/1970 12:00'.
I actually need as an empty string in variable abc if the maintenance.actualEndDate is 0
I'm working on angular 4, is there any optimal solution to this?
If what you need is converting non-zero timestamps to a formatted date, and zero timestamps as empty string, a dedicated function would be nice:
function formatTimestamp(secondsSinceEpoch) {
return secondsSinceEpoch===0 ? '' : moment.unix(secondsSinceEpoch).format('DD/MMM/YYYY HH:mm');
}
//...
let abc = formatTimestamp(maintenance.actualEndDate);
(But this has nothing specific to angular)

Total number of days in angular?

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.

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