I'm trying to compare two dates for exactly one month. I used the code below for the validation. Its working for this case:
from Date:1/5/2013 to Date:1/6/2013
but not working if we consider February month, e.g.:
fromDate:28/2/2013; toDate:31/3/2013;
Can you let me know the solution?
I tried the code below (source), but it's not working.
var fromDate = new Date(document.getElementById("Billing_From").value);
var toDate = new Date(document.getElementById("Billing_To").value);
fromDate.setMonth( fromDate.getMonth() + 1 );
if((fromDate-toDate) !=0)
{
alert("Please limit the date range to 1 month.");
}
Keep the following in mind.
The default format is MM/dd/yyyy.
In Javascript Date months are from 0(Jan) to 11(Dec).
This code is for comparing same dates of adjacent months. Example - 15th Feb, 2013 and 15th March, 2013.
What is you exact definition of month ?
Related
I am creating a platform for recurring monthly orders.
I am using later.js for the recurrence. I have come across the following two cases and I am wondering if anybody has suggestions on how to better handle these (or if later.js handles them natively somehow):
later.parse.recur().on(31).dayOfMonth()
The date is the 31st of a given month. Current result is that is jumps months that end on the 30th. WORKAROUND: is to use last().dayOfMonth().
later.parse.recur().on(30).dayOfMonth()
later.parse.recur().on(31).dayOfMonth()
Month of February, ending on the 28th or 29th. How to handle if the date is 30th (or 31st). WORKAROUND: If date > 28th, add .and().on(59).dayOfYear()
Thanks!
I don't know the specifics of later.js, but apparently you can write something called a custom modifier: https://github.com/bunkat/later/blob/master/example/modifier.js
In addition to this, if you add a month to a javascript date (doesn't matter if the number becomes greater than 11/december), set the day of the month to the first then subtract 1 day, then you'll get the date of the last day in the originally given month. For example:
var a = new Date("2000-02-25");
var b = new Date(new Date(a.getFullYear(),a.getMonth()+1,1)-1);
console.log(b);
I have a rails-generated date, and a jQuery-generated date.
The rails date prints as such: 2002-10-27
and the jQuery date prints as such: Tue Aug 14 2001 00:00:00 GMT-0500 (CDT)
I want to check if the jQuery date is greater or less than the rails date. But no matter the dates, the jQuery date is always interpreted as larger than the rails date.
Why is that, and how can I successfully compare the two dates?
var year = 2001
var month = 9
month --
var day = 14
var date = new Date(year, month, day);
<% #date = Date.today - 18.years %>
if( date > <%= #date %> ) {
//this code is always executed, no matter what dates I choose
}
UPDATE:
Actually I just figured out the problem is that it only allows dates before 1969. I intended the code to only allow dates over 18 years old. Does anyone know why the difference?
UPDATE 2:
I tested the time output of October 5th, 2000 in my js console and rails consoles, and they give the same first six digits, but the js console adds three zeros.
var year = 2000
var month = 10
month --
var day = 5
var date = new Date(year, month, day);
date.getTime();
=> 970722000000
Date.new(2000,10,5).to_time.to_i
=> 970722000
So it turns out the issue is that the js console prints times in milliseconds, which is why I was getting 973404000000, versus 973404000 in the rails console.
All I had to do was divide the js time by 1000, and comparing the js time to the rails time works perfectly.
var year = 2000
var month = 10
month --
var day = 5
var date = (new Date(year, month, day).getTime() / 1000);
date
=> 970722000
Date.new(2000,10,5).to_time.to_i
=> 970722000
User date Format patterns for Jquery Date, for changing the format of date according to ruby date , or second option is convert ruby date format according to jquery , using strftime.
You might try converting them both to their unix timestamps and comparing those. If you don't care about the hours, and simply the dates, it should work.
var year = 2001
var month = 9
var day = 14
var date = new Date(year, month, day);
<% #date = Date.today - 18.years %>
if ( date.getTime() > <%= #date.to_time.to_i %>) {
// do something
}
I'd use a library like Moment.JS to handle your date parsing needs on the client side. And then send the server something in a standard format like ISO8601 to ensure you don't have any problems in misintrepretation.
Epoch time will work as well, but as you've seen you have to carry the burden of ensuring that they're in the same units.
To compare 2 dates for me u can use moment.js
With ur rails date create a moment date. Do the same with ur jquery date.
U can compare easily the 2 dates now.
If u need help see this post : moment-js-date-time-comparison
I'm trying to validate a uk date using this code:
function ukdate(d) {
var p = new Date(d.split('/')[2], d.split('/')[1] -1, d.split('/')[0]);
if(p.toString() !== 'Invalid Date') {
return p;
}
}
http://jsfiddle.net/GE3xU/1/
so if I try ukdate('31/12/1981') it correctly returns "The Dec 31 1981". However if i try ukdate('12/31/1981') it returns "Tue Jul 12 1983".
Why is this happening? I'm expecting the second test to return invalid date because 31 is not a valid month.
JavaScript is converting your date for you.
In simple examples, you can get the last day of a given month by asking for the 0th day of the following month. Similarly, the "32nd of August" would be corrected to the 1st of September.
Months work similarly. The 13th month of a given year is the 1st month of the next. The 0th month of a year is December of the previous.
31 % 12 = 7, hence July, and floor(31/12) = 2 hence the year being shifted forward by two.
This is intended behaviour for JavaScript.
May I interest you in <input type="date" />? It uses whatever format is defined on the user's computer (ie. it is "locale-aware"), which is already excellent for user experience. On top of that, supporting browsers will render a calendar date picker, especially useful on phones too. Internally, the date is in "standard" YYYY-mm-dd format.
The month value is divided by 12 and added to the year, then the remainder is used as the actual month value.
See the spec
Let ym be y + floor(m /12).
Let mn be m modulo 12.
Given a Year, and a day of that year, how can i get the full date? ex: 60/2014 = March First 2014 and 61/2016 = March First 2016
notes: -year and day can be passed as separate parameters.
-result can be a regular Date instance, i just explicitally showed as text to alert for "leap year"
JavaScript has some pretty neat "magic":
var input = "60/2014";
var parts = input.split("/");
var d = new Date(parts[1],0,parts[0]);
That's right. 60th of January. JavaScript will automatically correct this to the first of March.
I have been using Stack Overflow for a number of months now, but this is my first post.
I require a function to convert a week number and and day of week into a dd/mm/yyyy format.
The date values i have to work with are in the format day/weekNumber. So for example: 3/43 converts to Wednesday 24 October 20XX. The year value will be the current year.
The day value starts at 1 (Monday).
I have found lots of functions on the internet (such as this, this and this). Some work with ISO 8601 dates, which i do not think will work for me. And i have not yet found one that works for me.
Thanks in advance,
This solution does require an extra library to be added, but I think it is really worth it. It is a momentjs library for manipulating dates and time. It is actively maintained and has a great documentation. Once you get the values for day and weekNumber (in our case 3 and 43), you should do as follows:
function formatInput(day, weekNumber){
var currentDate = moment(new Date()); // initialize moment to a current date
currentDate.startOf('year'); // set to Jan 1 12:00:00.000 pm this year
currentDate.add('w',weekNumber - 1); // add number of weeks to the beginning of the year (-1 because we are now at the 1st week)
currentDate.day(day); // set the day to the specified day, Monday being 1, Sunday 7
alert(currentDate.format("dddd, MMMM Do YYYY")); // return the formatted date string
return currentDate.format("dddd, MMMM Do YYYY");
}
I think this library might be useful to you later on and there are plenty of possibilities regarding date and time manipulation, as well as formatting options. There is also a great documentation written for momentjs.
So assuming you have the values of 3 and 43 separately, you can just do some simple maths on the first day of the current year:
Get 1st January Current Year
Add (43 * 7 + 3)
Something like this maybe:
var currentDate = new Date();
var startOfYear = new Date(currentDate.getFullYear(), 0, 1);//note: months start at 0
var daysToAdd = (43 * 7) + 3;
//add days
startOfYear.setDate(startOfYear.getDate() + daysToAdd);
Here is an example
EDIT
On second thoughts, I think I was wrong with your requirements. It seems you require a specific day of the week. Check this out for a better solution.
The problem is that it all depends on your definition of a week. This year starts on a sunday, so does that mean that 02/01/2012 (the first monday of this year) is the start of the second week?
My latest example will first find the start of the specified week, and then find the next occurrence of the specified day
According to ISO when dealing with week dates, the week starts on Monday and the first week of the year is the one that contains the first Thursday of the year. So for 2012, the first week started on Monday, 2 January and the first week of 2013 will start on Monday, 31 December 2012.
So if 3/43 is the third day of the 43rd week (which is the ISO date 2012-W43-3), then it can be converted it to a date object using:
function customWeekDateToDate(s) {
var d, n;
var bits = s.split('/');
// Calculate Monday of first week of year this year
d = new Date();
d = new Date(d.getFullYear(),0,1); // 1 jan this year
n = d.getDay();
d.setDate(d.getDate() + (-1 * n +( n<5? 1 : 8)));
// Add days
d.setDate(d.getDate() + --bits[0] + --bits[1] * 7);
return d;
}
console.log(customWeekDateToDate('3/43')); // 01 2012-10-24
Note that this uses dates, otherwise daylight saving changeovers may result in the wrong date.