How to validate date in javascript - javascript

I have a SharePoint Date Picker Control which is used to select a BirthDate . I want the birthdate to be atleast 16years older from the current date.
Can anyone guide me how to validate this using javascript? I have not used javascript much, Please guide me how to go about the validation process?

Look at using the library date.js:-
http://www.datejs.com/
This library adds a number of methods for working with date objects.
Without using an external library, the easiest way to compare a date of birth is over 16 is to convert the date to an epoch timestamp and then compare this against a timestamp of now minus 16 years.
Be aware that while most languages/platforms use timestamps in seconds, Javascript uses milliseconds, so when comparing against timestamps from external sources, you need to divide by a factor of 1000.

Related

Dealing with time in different timezones react/javascript (PST/PDT)

So I have this use case where a user should be able to enter data into a react site having an order_fulfilment_datetime_str which is supposed to be a RFC 3339 datetime string according to the specs of the api this would be posted to.
So essentially a user(in any timezone) selects a (future) date and time (using a plugin such as react-datepicker) assuming that the date time is going to be in PST/PDT.
But the plugin and javascript date object returns the selected date time in the browser's timezone. So I have two things to solve:
How do I make sure that selected date/time is in PST/PDT
How to deal with DST?
Currently what I am trying to do is to simply get individual date, hours, minutes using getHours(), getMinutes(), getDate() to form the string. But again then how will I find out if this selected datetime has offset of -8 or -7(i.e. PST/PDT)?
I checked date-fns and date-fns-tz library but was not sure how to solve this issue using these.
You can use luxon library for setting a global timezone that you want, and then whenever a user selects a date-time using a date-time picker you can simply convert that into the timezone you want.
import { Settings } from "luxon"
Settings.defaultZone = <time-zone>
The above code snippet will set a default zone for the app
Now when a user selects a date-time convert it to the timezone you want using the below code
DateTime.fromISO(dateTimeValue, { setZone: true})

Displaying Different Time-Zone in Single HTML Page

I want do display Dates from different time-zone (PDT, IST, JST and AEST) on single HTML page. I'm not very proficient in JavaScript learning currently. Any help is really appreciated.
In case you want to display current date, the javascript Date() will automatically get the date based on local timezone.
var date = new Date();
You can also refer to all the methods javascript date supports here -> https://www.w3schools.com/jsref/jsref_obj_date.asp
Best way to handle dates is by using moment library
https://momentjs.com/timezone/

RobinHerbots jquery.inputmask date range

I am using RobinHerbots/jquery.inputmask library.
I need to make sure the date inputted in great than current date.
There is code for yearrange but I dont see anything for basic 'daterange'.
Using RobinHerbots/jquery.inputmask library, how can I make sure the date entered is greater than current/todays date new Date()?
There is an open issue about your question in the github of RobinHerbots/jquery.inputmask:
RobinHerbots answers that this feature will be implemented in 3.4 and that for now you can use the postValidation callback.
In the current date alias this is not implemented. However, you can use the postValidation callback to validate if the input is after the current date.
It would be a nice feature, and I will add it to the date alias in milestone 3.4

Save time of day as number in mongodb, but display in a human format (using meteor autoform)

In a form, I'm asking for a start time & end time, which I'll be using in a script later on.
I figured in MongoDB, a time of day is best stored as the # of seconds since midnight (per How can I store time-of-day in MongoDB? As a string? Give arbitrary year/month/day?).
My question is: how can I display a human-readable time in autoform (e.g. 7:30pm) yet still save it as a number in mongodb & have proper client-side validation (make sure time is before 8:00pm)? I figure I could either use a datetime object & subtract the seconds since 1970, or I could parse the time string & do math on the hours, minutes, AM/PM.
Thoughts on methods? & where to put the math hooks in autoform? This seems like something folks a lot smarter than me have probably already figured out!
new Date(<unixTimeStamp>) will get you a javascript Date object which can be easily played with using libraries like moment (https://github.com/moment/moment)
Edit: also, to get the proper timestamp you can do +Date.now(). This jives with mongodb's date type

Multiple date compare in javascript

I am trying to validate, get and compare 2 dates.
The dates are coming from 2 text inputs and are currently formatted as the following example: 17/01/2011 00:00
How do I convert that string to a date using the Date object?
I'm trying to validate it under these terms:
Date must be in the correct format (17/01/2011 00:00)
Date must be in the future (How do I do that considering JS runs on Local and date can be set incorrectly on the user's machine?
First date must be before second date. (it's a from_date -> to_date input).
Can you please assist?
Thanks,
A slightly different approach,
If it is not too late, you can enforce the validity of the date by providing a date / time selection control that will output the date in the format that you required (There should be quite a lot of them out there, see this or this for some. In this way, you don't have to rely on the user to input the date in a textbox where they can input whatever they want. If this is not possible, you can try looking at the input masking that might help the user input the date more accurately in a textbox format, for example see: this or even this that will allow you to input string like "today", "2 weeks from now", etc. This will increase the usability aspect of the form.
You might have to revalidate the date on the server side for this. It's always a good idea to double check user inputs both at client side and again in server side. But for client side validation, you can try the datejs libray i mentioned above or create one as per suggestion from Stefanos.
Already answered this in #2.
Hope that helps.
As for the format validation you can use a regex to do it.
To get the current time you need to request the data from a server. One option is from the one your users get the web pages using a backing language (php, java, ...). One other option is to request the time from a time server with an http request. The answer could be simle text or an XML, or you may need to communicate with another protocol, like SOAP.
To compare the 2 dates you can convert them to milliseconds and then simply compare the values. Also the JavaScript object Date can be helpful for conversions and comparisons.
You cannot manipulate times and dates without knowing the time zone.
If a string representation of a date does not include the GMT offset, you have to guess the time-zone, unless you add a rule to account for it, or only want the date to be within a day or so accurate.

Categories

Resources