moment.js - isBetween() method doesn't work consistently - javascript

I have a moment.js code that works well:
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(moment(), moment().add(30, 'days'));
// returns true, that's great!
But when I start refactoring to make it more readable it fails to work:
var today = moment();
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(today, today.add(30, 'days'));
// returns false.. but why?

var today = moment();
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(today, moment(today).add(30, 'days'));
You are passing a reference which you have edited by adding 30 days.

Related

moment js doesn't return the correct date given week+year

I'm using this code:
var year = 2018;
var week = 1;
var currentDate = moment().day("Sunday").year(year).week(week).toDate().format("dd/MM/yyyy");
currentDate result is 06/01/2018 (Saturday) instead of 07/01/2018 (Sunday).
Do you know how to fix this problem?
Thanks,
Yael
Try to use toDay instead of toDate. And don't forget to check time zone too.
I changed the order and now it works well for the given example:
var currentDate =
moment().year(year).week(week).day("Sunday").toDate();

I want to store the value of a datePicker in WixCOde

Based on this Supplied Code,
$w.onReady(function () {
//TODO: write your page related code here...
const startFromDays = 4;
const endAtMonths = 9;
const today = new Date();
let startDate = new Date(today);
let endDate = new Date(today);
startDate.setDate(startDate.getDate() + startFromDays);
endDate.setMonth(endDate.getMonth() + endAtMonths);
$w.onReady(function () {
$w("#datePicker1").minDate = startDate;
$w("#datePicker2").maxDate = endDate;
});
});
I need help to find the difference between the endDate and the startDate and output it as Text. Knowing the fact that some start dates can be of the Eg: 26th of Feb and end Date can fall on 3rd March.
This Code is been run on Wixcode, where the dates are used as a Date-picker user input. Thank you.
Start by getting the difference between the two dates using something like what is described in this post.
Then, use that number to populate a text field that you've added to your page.
So, assuming you have a datediff() function declared:
const diff = datediff(startDate, endDate);
$w("#text1").text = diff.toString();

Format date and Subtract days using Moment.js

I would like a variable to hold yesterday's date in the format DD-MM-YYYY using Moment.js. So if today is 15-04-2015, I would like to subtract a day and have 14-4-2015.
I've tried a few combinations like this:
startdate = moment().format('DD-MM-YYYY');
startdate.subtract(1, 'd');
and this:
startdate = moment().format('DD-MM-YYYY').subtract(1, 'd');
and also this:
startdate = moment();
startdate.subtract(1, 'd');
startdate.format('DD-MM-YYYY')
But I'm not getting it...
You have multiple oddities happening. The first has been edited in your post, but it had to do with the order that the methods were being called.
.format returns a string. String does not have a subtract method.
The second issue is that you are subtracting the day, but not actually saving that as a variable.
Your code, then, should look like:
var startdate = moment();
startdate = startdate.subtract(1, "days");
startdate = startdate.format("DD-MM-YYYY");
However, you can chain this together; this would look like:
var startdate = moment().subtract(1, "days").format("DD-MM-YYYY");
The difference is that we're setting startdate to the changes that you're doing on startdate, because moment is destructive.
var date = new Date();
var targetDate = moment(date).subtract(1, 'day').toDate(); // date object
Now, you can format how you wanna see this date or you can compare this date with another etc.
toDate() function is the point.
startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');
Try this:
var duration = moment.duration({'days' : 1});
moment().subtract(duration).format('DD-MM-YYYY');
This will give you 14-04-2015 - today is 15-04-2015
Alternatively if your momentjs version is less than 2.8.0, you can use:
startdate = moment().subtract('days', 1).format('DD-MM-YYYY');
Instead of this:
startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');
startdate = moment().subtract(1, 'days').startOf('day')
In angularjs moment="^1.3.0"
moment('15-01-1979', 'DD-MM-YYYY').subtract(1,'days').format(); //14-01-1979
or
moment('15-01-1979', 'DD-MM-YYYY').add(1,'days').format(); //16-01-1979
``
I think you have got it in that last attempt, you just need to grab the string.. in Chrome's console..
startdate = moment();
startdate.subtract(1, 'd');
startdate.format('DD-MM-YYYY');
"14-04-2015"
startdate = moment();
startdate.subtract(1, 'd');
myString = startdate.format('DD-MM-YYYY');
"14-04-2015"
myString
"14-04-2015"

How do I get the time in milliseconds from 2 different string?

I have the following code which I get from parameters in the URL.
This is what I have in the URL
&dateStart=15.01.2015&timeStart=08%3A00&
After getting the parameters I have the following: 15.01.2015:08:00
Using Javascript how can I parse this string to get the date in milliseconds?
Date.parse(15.01.2015:08:00)
But obviously this doesn't work.
Date.parse(15-01-2015)
This works and I can change this but then how do I add or get the milliseconds from the time??
This is quite possibly the ugliest JavaScript function I've written in my life but it should work for you.
function millisecondsFromMyDateTime(dateTime) {
var dayMonth = dateTime.split('.');
var yearHourMinute = dayMonth[2].split(':');
var year = yearHourMinute[0];
var month = parseInt(dayMonth[1]) - 1;
var day = dayMonth[0];
var hour = yearHourMinute[1];
var minute = yearHourMinute[2];
var dateTimeObj = new Date(year, month, day, hour, minute, 0, 0);
return dateTimeObj.getTime();
}
It will work with the format that your DateTime is in aka day.month.year:hours:minutes.
You can achieve it using Javascript Date Object and JavaScript getTime() Method:
var dateString="01.15.2015 08:00";
var d = new Date(dateString);
console.log(d);
var ms=d.getTime();
console.log(ms);
ms+=10000;
console.log(new Date(ms));
Here is a DEMO Fiddle.
Note: Change your date string from 15.01.2015:08:00 to "01.15.2015 08:00" because it's not a valid Date format.
Check for format
Date() in javascript :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Format allowed :
https://www.rfc-editor.org/rfc/rfc2822#page-14
You can try to use moment.js library like this:
moment('15.01.2015 08:00', 'DD.MM.YYYY HH:mm').milliseconds()
Just for the sake of completion, you can always extract the information and create a Date object from the extracted data.
var dateStart = '15.01.2015'
var timeStart = '08:00';
var year = dateStart.substring(6,10);
var month = dateStart.substring(3,5);
var day = dateStart.substring(0,2);
var hour = timeStart.substring(0,2);
var mins = timeStart.substring(3,5);
var fulldate = new Date(year, month-1, day, hour, mins);
console.log(fulldate.getTime());

momentJS date string add 5 days

i have a start date string "20.03.2014" and i want to add 5 days to this with moment.js but i don't get the new date "25.03.2014" in the alert window.
here my javascript Code:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add("DD-MM-YYYY", 5);
alert(new_date);
here my jsfiddle: http://jsfiddle.net/jbgUt/1/
How can i solve this ?
I like this string format "25.03.2014"
Hope someone can help me.
UPDATED: January 19, 2016
As of moment 2.8.4 - use .add(5, 'd') (or .add(5, 'days')) instead of .add('d', 5)
var new_date = moment(startdate, "DD-MM-YYYY").add(5, 'days');
Thanks #Bala for the information.
UPDATED: March 21, 2014
This is what you'd have to do to get that format.
Here's an updated fiddle
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');
alert(day + '.' + month + '.' + year);
ORIGINAL: March 20, 2014
You're not telling it how/what unit to add. Use -
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
moment(moment('2015/04/09 16:00:00').add(7, 'd').format('YYYY/MM/DD HH:mm:ss'))
has to format and then convert to moment again.
The function add() returns the old date, but changes the original date :)
startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days');
alert(new_date);
You can add days in different formats:
// Normal adding
moment().add(7, 'days');
// Short Hand
moment().add(7, 'd');
// Literal Object
moment().add({days:7, months:1});
See more about it on Moment.js docs: https://momentjs.com/docs/#/manipulating/add/
var end_date = moment(start_date).clone().add(5, 'days');
If we want to use the current date or present date:
var new_date = moment(moment(), "MM-DD-YYYY").add(7, 'days')
alert(new_date);
To get an actual working example going that returns what one would expect:
var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
var thing = new_date.add(5, 'days').format('DD/MM/YYYY');
window.console.log(thing)
add https://momentjs.com/downloads/moment-with-locales.js to your html page
var todayDate = moment().format('DD-MM-YYYY');//to get today date 06/03/2018 if you want to add extra day to your current date
then
var dueDate = moment().add(15,'days').format('DD-MM-YYYY')// to add 15 days to current date..
point 2 and 3 are using in your jquery code...
If you do end up running with formatting problems after adding X time to the function, try this format:
startDate = moment(startDate).add(1, "days").format("YYYY-MM-DD");
instead of:
startDate = moment(startDate, "YYYY-MM-DD").add(1, "days");
This last version keeps the time attached to the returned data, whereas the format method doesn't and literally returns YYYY-MM-DD.
You can reduce what they said in a few lines of code:
var nowPlusOneDay = moment().add('days', 1);
var nowPlusOneDayStr = nowPlusOneDay.format('YYYY-MM-DD');
alert('nowPlusOneDay Without Format(Unix Date):'+nowPlusOneDay);
alert('nowPlusOneDay Formatted(String):'+nowPlusOneDayStr);
updated:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add(5,'days');
alert(new_date)

Categories