Using moment, I'm trying to create an if statement to see if their joinDate is within 14 days.
let joinDate = ${moment(member.joinedAt).format("YYYY, MMMM Do dddd, HH:mm:ss")}
That's my current code to format when the user joined.
Basically what I'm saying (if you're not following) is how would I check if joinDate is within the last 14 days?
You may wanna use momentjs diff function to get the days difference between the current date and the input .
A Snippet for your reference
const selectedDate = "2021-08-07";
const date_temp = moment().diff(selectedDate, "days", true);
console.log('Is within 14 days? ',date_temp >= 0 && date_temp <= 14);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Related
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
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');
I am facing same problem, by using above mentioned code problem is still not resolved.
My date is coming in JSON like
200515
where 20= date
05= month
15= year,
I am using the following code:-
{{200515 | date : "EEE, MMM dd"}}
but it gives me:
Thu, jan 01
but I want
Wed, May 20
Please help for the same.
You have to create a date object before using your filter because it will only formatted the date. Here your date is null.
Firstly you have to create your date using basic Date api
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
and after use the created object in your code with the filter
Your input string for your date 200015 is not a valid ISO 8601 date string. You must supply either a javascript Date object or a valid string of format such as 'yyyy-MM-ddTHH:mm:ss.sssZ' or 'yyyy-MM-ddTHH:mmZ' etc. See http://en.wikipedia.org/wiki/ISO_8601
Just use .substring() to parse your date string and build a new date object. This works with angular's date filter.
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
var date = '200515',
day = date.substring(0, 2),
month = date.substring(2, 4),
year = date.substring(4, 6),
time = '0513',
hour = time.substring(0, 2),
minute = time.substring(2, 4);
$scope.date = new Date('20' + year, month - 1, day, hour, minute);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp", ng-controller="MyCtrl">
{{date | date : "hh:mm EEE, MMM dd"}}
</div>
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.
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 ;)