I have one text box. When I enter a date with the format MM/DD/YYYY in that textbox and click outside the textbox I need to display the result as MM/DD/YYYY+3 using JavaScript.
For example, if my date is 12/31/2013 then the result would be 01/03/2014.
Hope this link will help you to find the answer:
adding-number-of-days-to-an-entered-date-in-javascript.
Have a look at the excellent library moment.js with which you easily can add three days to your original date.
In your case it would be something like:
var input = moment(myTextBoxValue);
input.add('d', 3); // input is now 3 days later
I guess the easiest to program and best readable solution would be to use a dedicated library for handling dates, such as Moment.js.
There, you have got the add function which allows you to add an arbitrary amount of time to a given point in time. E.g.:
moment().add('days', 7);
If you use the moment function to parse the time entered, use that as your source value, call add on it, and return it as JavaScript Date using the toDate function, you get exactly what you want.
So basically it comes down to:
var sourceDate = moment(new Date(2013, 7, 8)),
targetDate = sourceDate.add('days', 3),
result = targetDate.toDate();
Then, result contains the Date object you wanted to have.
I'd prefer that over native JavaScript handling of Date, as it is way more readble and hence understandable.
Related
I can hard-code the last day of a year because anyway it will always be 31st December. There are many solutions using date, js, jquery. But I'm working on an Angular project and hence my code is in typescript. My tech lead wants me to do this using moment. I'm not supposed to hard-code any date. Is there any built-in method provided by moment to fetch the last day of a given year. And I'm saying given year because in my case given year is dynamic. It can change anytime. I'm using endOf() method. I'm getting error with custom year. I mean:
const lastDayOfYear = moment().endOf('year') is working fine, but:
const lastDayOfYear = moment().endOf(2025) is giving me this error:
Argument of type '2025' is not assignable to parameter of type 'StartOf'
I also tried:
const lastDayOfYear = moment().endOf("2025");
year=2025; const lastDayOfYear = moment().endOf(year);
How will this method work? I've gone through the entire Moment docs. Or should I stick to Date of javascript. Something like this:
new Date(new Date().getFullYear(), 11, 31);
Please provide a solution.
PS: I want last date of a given year in MM-DD-YYYY format only.
Well if you really wanted to work that out with moment instead of using `12-31-${year}` you could instead use:
moment([year]).endOf('year').format('MM-DD-YYYY')
moment(date).endOf('year');
Where date is a Date somewhere in that year.
set current date in moment new Date()
const now = moment(new Date()).endOf('year');
alert(now);
Is there an easy way in moment.js to compare times with no date attached?
It works when I have a full ISO object with date+time, but not when it's only a time.
For example, this basic code doesn't work because, I presume, it expects a date in the value:
var time_obj = moment(active_time_format, "HH:mm:ss");
console.log(time_obj.isBefore('12:00:00'));
I understand that I can just add an arbitrary date in there, but what is the elegant way to do this?
Update:
This works from the comment below by #RichardHamilton:
var time_obj = moment(active_time_format, "HH:mm:ss");
console.log(time_obj.isBefore( moment('12:00:00', "HH:mm:ss") ));
Is there a way to parse the time in dateBox(), or better yet eliminate it all together? I want someone to be able to use the dateBox() to choose the date only, but want to get rid of the time and use another function I create for the time.
thanks
one simple way of doing this is like this :
var date = new Date(e.parameter.start);// convert the string to a full date object (with time value) (the date widget is named 'start' in this example)
var dateOnly = date.setHours(0,0,0,0); // set time to '0' (hours,min,sec,millisec)or use the hours and minutes you get from elsewhere...
You can have a look at this example sheet where I use this to combine data from 2 columns (date and time) to create a complete date object
I have a date of birth like 12-08-1989 in text box in HTML.I want to validate that the user must be of 18 years old in javascript.
I have used Date function in javascript but it seems like it accept YYYY-MM-DD format but i want to validate in DD-MM-YYYY.
How can I achieve this?
var pattern =/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/;
if(pattern.test(str_input_date))
{
alert("valid date");
}
This should give you a start. :)
You should probably be using some validation framework, like jQuery.validation. This is by far more consistent way to handle validation in your code. If this is the only validated field in your app, you can, of course, use naive implementation as provided above.
Whether you need some advanced date validation rules, you could use a JS date framework, like Moment.js (or Date.js, which is pretty outdated at the moment).
you can use the date class, just like this
try{
var x=new Date('1/1/2001');
var Cnow=new Date();//current Date
if(Cnow.getFullYear()- x.getFullYear()<18){
alert('not old enough');
}
else{
//success !!!
}
}
catch(ejs){alert('invalid')}
"I have used Date function in javascript but it seems like it accept YYYY-MM-DD format but i want to validate in DD-MM-YYYY"
You can create a Date object by passing the year, month and day (and, optionally, the hour, minute, second, and millisecond) values separately:
var aDate = new Date(2012, 5, 22);
So if you use a regex or other string methods to extract the pieces from what the user entered then you can create a date from DD-MM-YYYY (or whatever other) format you like.
The way I'd test if the user is at least 18 years old would be to add 18 years to their date of birth and see if that is on or before today's date:
aDate.setFullYear( aDate.getFullYear() + 18 )
The various methods available on a Date object are listed at MDN.
I need a way to turn my 2 character string dates (i.e. '04/10/2010' & '05/24/2010') into an integers to see if one is greater than the other. If the user enters an end date that is less than the begin date I need to popup an "invalid date range" error.
From what you posted your real problem is:
I need to see if one date is greater than another in javascript/jquery.
If so all you need to use is the Javascript Date object (how to page here).
You can use it as follows:
var dateTextA = '04/10/2010';
var dateTextB = '05/24/2010';
var dateA = new Date(dateTextA);
var dateB = new Date(dateTextB);
if (dateA < dateB){
alert("Your date is out of range!");
}
Note: Above code has been tested and works in IE7.
If you really feel you need an integer value, you can use the UTC function to get that.
The problem is Date.parse('04/10/2010') returns a timestamp (integer) for April 10 in the US and 4 October most other places.
It is best to use a less ambiguous format - if you are taking user input, give the user a calendar, menu, or three label inputs, then build the date from that.
3 inputs:
new Date(+fullyearinput, monthinput-1, +dateinput)
If Date won't parse what you are looking for, Datejs provides a lot of syntactic sugar to make your life easier.
To compare two dates, all you need to do is turn your strings into Date objects and then use the greater than, less than, or equality operators. Javascript provides Date comparison natively.