moment.js errors - javascript

I'm trying to replace the functions that I had previously built with date.js, using moment.js. I'm wrapping the dates in moment wrappers like this:
var start = moment(new Date(lastLoadDate.getTime()));
var end = moment(new Date(lastLoadDate.getTime()));
Then, I'm trying to set Datepicker values based upon lastLoadDate. So, for last month, I'm doing:
start = start.day(1);
end = end.day(1).add('months', 1).subtract('days', 1);
// format dates and set DatePicker values
start = start.format('MM/DD/YYYY');
$('.date_from', context).val(start);
end = end.format('MM/DD/YYYY');
$('.date_to', context).val(end);
This gives me my first error:
end.day(1).add is not a function
However, if I take out part of the end date manipulation
end = end.day(1);
I now get the error:
start.format is not a function
I'm using moment.min.js version 1.1.0.

Turns out that the documentation is wrong, and that function date() should be used instead of day() to set the date. Instead of writing
end = end.day(1).add('months', 1).subtract('days', 1);
either
end = end.date(1).add('months', 1).subtract('days', 1);
or
end.date(1).add('months', 1).subtract('days', 1);
will work, interchangeably.

Related

how to set end date when select the start date?

i want to set end date depend on the balance and start date when the user select the start date,
for example if the start date is 2021-09-01 the balance is 11 so direct the end date will be 2021-09-12
i'm using DatePicer from ant Design ,
this is my code
useEffect(() => {
if (
(selectedLeaveId && selectedLeaveId === LeavesId.annualLeaveId)
) {
const start = startDate;
// this will change the start date too , i dont want that
const directEndDate = start?.add(remainingBalance, 'days');
setEndDate(directEndDate);
}
}, [selectedLeaveId, startDate, remainingBalance]);```
Strongly recommend using moment.js
https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/01-add/
You can pass date as below
let start = moment(startDate)
let end = start.add(DIFFERENCE, 'days');
The problem you are having is because you are modifying the start date object and assigning it to end. I'm not up-to-date on the specifics of moment js but I'd imagine you can do something like:
let end = moment(start).add(DIFFERENCE, 'days');
That way you'll create a new date object for end and add to that one, rather than modifying start.
As a side note, if you aren't going to change what's in the start or end variables you'd be better off writing const start ... rather than let start ... (it will prevent certain bugs from creeping in). This won't prevent you from changing the objects through their methods, it just stops you from accidentally reassigning them.

How to get the difference between two dates with php and symfony 4?

I am developing a holiday management project. There is an entity called Conge which includes date_departure, date_retrun and period. I made a form and the view. I would like to set the difference between the date of departure and the date of return. For example, if the user chooses dates from '04-05-2019' to '04-08-2019', how can I get and display the difference in days using javascript, php, and symfony4?
If you are always going to use the m-d-Y format for your dates you can use just PHP to give you the number of days difference.
$departure ="04-05-2019";
$arrival = "04-08-2019";
$departure = DateTime::createFromFormat('m-d-Y', $departure)->getTimestamp();
$arrival = DateTime::createFromFormat('m-d-Y', $arrival)->getTimestamp();
echo ($arrival - $departure) / (24*60*60); // 86400 might save some math
Use momentjs (https://momentjs.com/docs/#/displaying/difference/)
var departureRaw = $(".departure").val();
var departureDate = moment(departureRaw);
var returnRaw = $(".return").val();
var returnDate = moment(returnRaw);
var difference = departureDate.diff(returnDate, 'days');
for php, as mentionned here and here you can use ->diff() function. I don't think than Symfony have any function for that

Change date format on a template

I'm using the Foundry template on Squarespace and I need to change the date format on post pages from english to portuguese. Instead of "May 6" I need "6 Mai". In Brazil we use the pattern dd/mm/yyyy. In this case I just want the day and month, and also translate all the months (to: Jan, Fev, Mar, Abr, Mai, Jun, Jul, Ago, Set, Out, Nov, Dez).
I already saw people solving this for others languages there. But not to portuguese or on the Foundry template. It's possible to make a code-injection on Squarespace, on the head or footer. I just need a Javascript that can do that, overwriting the theme's default date format.
I would approach it via the following Javascript, inserted via code injection. Note that although some of the month abbreviations are the same, I've included them for clarity and so that it may be more reusable for others. Also, the abbreviations I've used for the keys (that is, the original month abbreviations) may not be what Squarespace actually uses, so they may need to be updated.
<script>
(function() {
var dates = document.getElementsByClassName("dt-published date-highlight");
var newDate;
var i,I;
// Create object with 'source' keys on the left, and 'output' values on the right.
var months = {
"Jan":"Jan",
"Feb":"Fev",
"Mar":"Mar",
"Apr":"Abr",
"May":"Mai",
"Jun":"Jun",
"Jul":"Jul",
"Aug":"Ago",
"Sep":"Set",
"Oct":"Out",
"Nov":"Nov",
"Dec":"Dez"
};
// Loop through all dates, replacing months and reordering display.
// - Trim extra white space from beginning and end of date.
// - Replace multiple consecutive spaces with a single space.
// - Split by space into an array.
// - Replace month text based on 'months' object key:value pairs.
// - Convert array to string, rearranging display order of elements.
// - Set new date HTML.
for (i=0, I=dates.length; i<I; i++) {
newDate = dates[i].innerHTML.trim();
newDate = newDate = newDate.replace(/ +/g, ' ');
newDate = newDate.split(" ");
newDate[0] = months[newDate[0]];
newDate = newDate[1] + " " + newDate[0];
dates[i].innerHTML = newDate;
}
})();
</script>

React Native, Javascript : Simple function to return all dates between two dates, gives different result on simulator and real-life device

I have a simple function, when returns all dates between two dates.
See the code :
events(s, e){
let between = [];
let currentDate = new Date(s);
let end = new Date(e);
while (currentDate <= end) {
let dum = new Date(currentDate);
let dummy = moment(dum).format('YYYY-MM-DD');
between.push(dummy);
currentDate.setDate(currentDate.getDate() + 1);
}
return(between);
}
I'm using it in my react native app. there is one problem which happens only when I give start and end dates as 2016-10-22 and 2016-10-30.
In this particular case, when i run it in iOS simulator, the return array's length is 9. the array includes the start date and end date and all dates between.
I run the exact same app, exact same function and parameters, app in iPhone returns different results (only in this particular case while start and end dates are 2016-10-22 and 2016-10-30)
In iPhone, array's length is 8, and it's not including the last date.
Is there any logical problem in my function?

manipulating date object with javascript

i have been tinkering with the date object.
I want to add a dynamic amount of days to a day and then get the resulting date as a variable and post it to a form.
var startDate = $('#StartDate').datepicker("getDate");
var change = $('#numnights').val();
alert(change);
var endDate = new Date(startDate.getFullYear(), startDate.getMonth(),startDate.getDate() + change);
does everything correctly except the last part. it doesnt add the days onto the day
take this scenario:
startdate = 2011-03-01
change = 1
alert change = 1
endDate = 2011-03-11 *it should be 2011-03-02*
thank you to all the quick replies.
converting change variable to an integer did the trick. thank you.
parseInt(change)
just to extend on this: is there a way to assign a variable a type, such as var charge(int)?
You may have fallen victim to string concatenation.
Try changing your last parameter in the Date constructor to: startDate.getDate() + parseInt(change)
See this example for future reference.
convert change to a number before adding it. it looks like you're getting a string concatenation operation rather than the addition you're expectingin your code.
I believe you are concatenating instead of using the mathematical operator. Try this instead,
var endDate = new Date(startDate.getFullYear(), startDate.getMonth(),startDate.getDate() + (+change));
It looks like you are not adding the ending day, you are concatinating it so '1' + '1' = '11'
use parseInt() to make sure you are working with integers
example
var change = parseInt($('selector').val());
Also, with this solution, you could easily end up with a day out of range if you are say on a start date of the 29th of the month and get a change of 5

Categories