Im having problems when I try to compare 2 dates.
var end = moment(items[i].dateEnd).format('DD/MM/YYYY');
var now = moment().format('DD/MM/YYYY');
Example: now = '29/10/2015' and end = '30/06/2015'
Tried using .diff() function from moment.js without any result, like this:
end.diff(now);
Any help?
Thanks.
You can try with specifying input date format:
moment(items[i].dateEnd, 'DD/MM/YYYY').diff(moment());
Related
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
I'm really struggling to do a clean, efficient way of showing yearweek for Sunday to Monday
e.g.:
201624
201625
201626
201627
201628
Is there an efficient way to do so in Google Scripts or Javascript without using a library?
Thanks!
Found a solution! :
var yearMonth = parseInt(Utilities.formatDate(new Date(), SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "yyyyww")).toFixed(0);
It was provided by user: Balu Ertl at Get week of year in JavaScript like in PHP
I would use Moment.js. Documentation for the use can be found here for the format you are looking for http://momentjs.com/docs/#/displaying/.
To have it work with google script you will need to first add it to the libraries using MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48, then you need to load it in using something like var moment = Moment.load().
From here you can get the month and year already formatted into your requested format.
var date = getDate();
var wwyy = moment(date).format('wo, YY');
My appologies, my format wasn't exactly as you wanted it to be. To answer in the format you are looking for:
var date = new Date();
var yyyyww = moment(date).format('YYYY ww');
This is a string 2011-11-09 00:00:00
So now how do I separate the date i.e. 2011-11-09 from the string, I dont want to use the slicing here if anyone has better options or ideas please let me know..
var date = '2011-11-09 00:00:00'.split(' ')[0];
You can split it by ' ' and then the first element will contain what you want.
console.log('2011-11-09 00:00:00'.split(' ')[0]);
Like this:
var date = 'This is a string 2011-11-09 00:00:00'.match(/\d{4}-\d{2}-\d{2}/)
if you dont want to use splicing, like you posted you could create a Date obj
var newDate = new Date(2011-11-09 00:00:00);
then to get the date, just use the toString override
var dateOnly = newDate.toString("YYYY-mm-dd");
That is if you dont want to use splicing or splits
I want to develop a JavaScript function to calculate the activity of users based on the date in the server where the data is stored. The problem is that the date is a string like this:
2013-08-11T20:17:08.468Z
How can I compare two string like this to calculate minor and major time as in the example?
If you want to compare two dates just use this :
var dateA = '2013-08-11T20:17:08.468Z';
var parsedDateA = new Date(dateA).getTime();
var dateB = '2013-06-06T17:33:08.468Z';
var parsedDateB = new Date(dateB).getTime();
if(parsedDateA > parsedDateB) {
// do something
}
Assuming you need to do the comparisons client-side, the best way is to load the dates into Date objects using Date.parse. Then compare them using the functions provided for Date, such as getTime.
Try parse method:
var s = "2013-08-11T20:17:08.468Z";
var d = Date.parse(s);
As I have understood you in the right way, there is a good answer to your question here.
You can also look at this very good Library (DateJS).
If your problem was converting from the Date-String to js-Date look at this Page.
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