I'm using moment.js to handle my date time values in my web application. I'm using follow format: DD.MM.YYYY for example 29.09.2016. Now I have some input fields, where I can type in the day of birth of a person. When I type a short year between 62 and 99, it converts it like this for example: input = 29.09.82 it converts it after pressing tab to 29.09.2082 (puts 20 befor the 82). When I write a short year between 00 and 62 in converts it like this example: input = 29.09.55 would be converted to 29.09.1955 (puts 19 before the 55). So the first range will be convertet to a year in the 21th century and the second range would be convertet to a year in the 20th century (this seems to be default, I don't know why). Now I would like to prevent converting to a year, which is in the future (so not more than 2016, and just 2016 when the short year is 16). How can I do this? At this moment I have something like this:
if(value.isValid() && isDayOfBirth) {
value.format(myFormat);
}
Is there a way to do this or to check it? I didn't found anything in the http://momentjs.com/docs/. Thanks.
This should work.
You take the current year and check if the parsed year is greater than that.
If so, you subtract 100 years (bringing you back to 20th century).
var now = moment(),
year = now.year(); //Get current year
if (parsedDate.year() > year) {
parsedDate.subtract(100, "year");
}
Related
I am using HTML's <input type='date'>. The year it displays by default is 2021. However, I want people who is at least 13 years old to be able to access my app. Therefore, I want to modify the date in such a way that it always displays the default year as 13 years back and that should also be the last year to select from. For example, as we are in 2021 so people born no later than 2008 should be able to join. So the last year to be shown should be 2008 and this should increase every year automatically. I could do it easily if I had used jQuery datepicker but I am happy with HTML's default input type date option.
You can set the max date to 13 years before the current date programmatically by getting the current date (via the Date() constructor), setting the year to the current year - 13 and assigning it to the max property:
const d = new Date();
d.setYear(d.getFullYear() - 13);
date.max = d.toISOString().split("T")[0]; //this simply converts it to the correct format
date.value = d.toISOString().split("T")[0];
<input type="date" id="date">
On 1 jan 0099 there was Thrusday but it return. Friday
days = new Date(" January 1 ,0099")
day = days.getDay()
alert(day);
RESULT
5
But it should return 4
Basically, it appears Javascript won't construct a Date in the year 99:
year
Integer value representing the year.
Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Syntax
You can try with different formats, 99 always appears to map to 1999. Likely this was implemented as a workaround and/or “convenience” for Y2K dates, perhaps even inherited from Java.
I'm not sure if there's a better workaround, but this works:
let d = new Date(100, 0, 1);
d.setFullYear(99);
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'm running to an issue I'm not sure how to resolve it; so the deal is that I have a function of javascript that take the date selected from the user and depending of another selection it adds to the date 6 o 12 months, the weird thing is that when you select a date of June, and make it add the 6 months I get a return value of 0 months, and it should be 12 for December, also If you choose December and add 12 months I'm getting a 0 as a return value. So here is my code
var date1 = $("#date_begin").val();
var days= date1[0] + date1[1];
var month= date1[3] + date1[4];
var year= date1[6] + date1[7] + date1[8] + date1[9];
var actualDate = new Date(year,month,days);
actualDate.setMonth(actualDate.getMonth() + 6);//add 6 months
$("#date_finish").val(actualDate.getDate()+"-"+actualDate.getMonth()+"-"+actualDate.getFullYear());
I'm printing the date directly to the text box as you can see. Also for the selection of the date in the first box I'm using the datepicker of jquery with this option selected
$("#date_begin").datepicker("option", "dateFormat", "dd-mm-yy");
I have been trying to fix this but I have no idea how to do it.
Hope you guys can give me a hand.
Months are indexed from 0.
June is 5 not 6.
What you have calculated is actually July (6) + 6 months = January (0) of the next year.
If you can get the date string into a format accepted by Date's constructor, that might be an easier way to create your date object. Though you need to be aware that there are some differences between browsers when it comes to the formats that are accepted.
If the date comes from users, then you should use a date format that makes sense for your users, and if necessary use a library that can work with that format of date.
When the date comes from communications with the server, I like to use the same format produced by JSON.stringify (dates that look like 2015-06-09T21:42:25.816Z), and I use es5-shim.js to make sure that the new Date(string) constructor can read strings in that format.
There is some information about parsing date strings using the new Date(string) constructor on Mozilla's developer site.
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.