I am working on a health website.
I am having a field called Last Menstrual Period, its a textbox which has to be filled in by doctor in format of YYYY-MM-DD.
What I want to do is, I have to add 281 days into the LMP date that the doctor will be entering in order to generate the Expected Delivery Date (Child Birth)
So I need followings thing to do:
The date entered should be in format of YYYY-MM-DD
It should be a valid date i.e. taking care of leap years and invalid dates like 2013-02-31 etc.
After generating the Expected Date of delivery (based on LMP that is entered), it should be displayed on screen.
As soon as the date is entered in the LMP textbox, these validations should be performed and the Expected Date Of Delivery is displayed inside a tag below the LMP textbox
How can I do that? Here is what I have tried so far.
// Calculate Expected Date Of Delivery
$('#lmp_date').change(function()
{
var lmp_entered = $this.val();
if(lmp_entered)
{
// $('#edd').html('1');
}
else
{
// $('#edd').html('Please enter last menstrual date to calculate EDD');
Please help me how to implement these validations and display the date using jquery. I am a newbie in jquery so dont know much about it. Any small help will be highly appreciated.
for validation I don t recommend using another library especially for such a simple rule. if the rule is so strict just write your own and use it site wide.
For example block non-numberic entries to textbox and add dash "-" by your code in every 5th and 9th chars.
Then to validate check the length of text and dashed, use split to get year. month and day separately and convert it to date with Date(txtYear,txtMonth,txtDay) if your date's year, month and day are equals to your txtYear, Month and Day then it means it s a valid date. After that use the below to calculate birth date.
just use Date constructor like below
var delDate = new Date(year, month, day + 281)
Just don t forget months start 0 in JS so Jan = 0, Feb = 1 etc.
Related
I need validation for the date field in angular8 or 10
Format- DD/MM/YYYY
Conditions:
->First, if the user enters [32] it should show an error message because max dates are 31 only every month.
->if he enters [31/02] then again, it should show an error because February doesn't have 31 days like this for every month
->If he enters [21/01/156277] again, it should show an error because the year should contain only 4 digits at last.
->I need validation in this way
->After date and month slash symbol should apply automatically if it is not the possible user should enter manually
->please help me with this https://stackblitz.com/edit/angular-bstzvt?file=src%2Fapp%2Fdatepicker-overview-example.ts
->I have written some conditions but not getting where to apply them please help me with this
i try so much and i think find it. see this :
https://stackblitz.com/edit/angular-bstzvt-hnusdx?file=src/app/datepicker-overview-example.html
I'm currently having a problem. I have two date-time fields a Start Date and an End Date. If i pick a date on the Start date for example October 2,2016 and pick a date on the End date October 1,2016 an alert dialog box should appear that the end date is earlier than the Start Date. How do i validate this via script? Sorry i am still very new to programming.
try this datetime lib, it has such function
http://momentjs.com/docs/#/query/is-after/
it shoud be something like
if (moment(dateBefore).isAfter(dateAfter)) {do somth}
I have a couple of textboxes where a user set a from date and to date from a button click which adds 7 days or substracts 7 to whatever is the current value in each box.
When the page is first loaded the dates that are added into the textboxes are based upon the user belonging to a group. Thus if a user belongs to group A, the from date is a Sunday, but if the user belongs to group B it is a Friday. This logic I set in the page load event in my ASP.Net page.
The situation that is baffling me, is whilst I can set the dates, and get my JavaScript to work with group A, if a switch the user to group B, and click on a button rather expecting the date in from changing from 17th January 10th January, it jumps to 29th December 2014. It seems to be getting a completely differently value initially 5th January 2015.
The JavaScript I have is:
var fromDateIn = new Date(formatDate(document.getElementById('<%=txtFromDate.ClientID%>').value));
var newfromdate = new Date(fromDateIn);
In one of the button event, I have:
function setNewFromDate() {
newfromdate.setDate(newfromdate.getDate() - 7);
document.getElementById('<%= txtFromDate.ClientID%>').value = formatDate(newfromdate);
var toDate = new Date(newfromdate);
document.getElementById('<%= txtToDate.ClientID%>').value = formatDate(toDate.setDate(toDate.getDate() + 6));
}
As I say, everything works perfectly and I get the right dates when the user is one group, but as soon I change the user to another group and set the initial from and to dates, I get this problem. Can anyone please advise I can get consistency in this?
Thanks
There is a problem with the way you are instantiating fromDateIn. You are passing it a value in the format dd/MM/yyyy, but the date constructor that takes a single string value is locale dependent and is treating the input as MM/dd/yyyy. This results in a completely bogus value for fromDateIn that throws everything else off.
You should find a way to determine the year, month and day for the start date (either by parsing it out of the textbox value or having the ASPX logic stuff it into the page somehow). And instantiate the date using the new Date(year, month, day) constructor. (This requires bearing in mind that month is 0-based [i.e. January = 0, February = 1]).
This line:
newfromdate.setDate(newfromdate.getDate() - 7)
Looks like you're trying to move back a week. This will fail. setDate changes only the Day of Month. Can you guess what happens when Day of Month is less than 7?
Okay, I have heard about it but I can confirm now that the Javascript Date functionality is a disaster zone. And I have created a monster out of it. I have this Program :
A JSON object contains list of holiday dates and its respective label.
I need to find out the date of 5 business days from today (excluding saturday, sunday and holiday if any which is contained in the JSON object.) Good stuff so far. Then this 5 business days' date is going to be devoured by the jquery calender as a default selected date which is not included in the fiddle as it is irrelevant. (Note: the start date on the calender is tommorow's date) Good stuff again. THEN, comes this part: If it is before noon today, I can select tommorow else start date is day after tommorow. I'm elaborating this because it is included in this fiddle.
So the problem is multiple initialization of the function which handles above functionality is not producing consistent result. It was calculating 5 business days on my system, but when i made this fiddle, it is calculating 4. The date of "5th" business days is incremental by 1 on each call.
http://jsfiddle.net/xXQ7j/27/
Anyone!
Your problem is probably caused by timezone issues.
Whenever possible you should use new Date(y, m, d) to create a date object, rather than supplying a string. In particular, I've found that you get a date relative to 00:00 UTC if you specify a string in format yyyy-mm-dd but one relative to local midnight if you use yyyy/mm/dd.
In any event, I would suggest a different approach:
convert your holiday date into an object, with the date being the key
generate today's date
if it's after noon, get tomorrow's date - d.setDate(d.getDate() + 1)
create an empty array
add one day (per #3 above)
check if the new day is Saturday or Sunday, if so, go back to #5
check if the new day is in the holiday list, if so, go back to #5
add the new date to the array
repeat until you have 10 entries
That should give you the next 10 business days in your array. Pick the ones you need to fill out your date picker.
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.