Im trying to validate a birthdate with moment js, but i cant do it.
Im using this code:
dateIsBefore(date) {
return moment(date, 'DD/MM/YYYY').isBefore(moment().format('DD/MM/YYYY'));
}
and this is my masked input element:
<el-input
class="date-input"
type="tel"
v-mask="'##/##/####'"
placeholder="dd/mm/aaaa"
></el-input>
but it return me a false value, when i expecting a true. The moment object say the date is invalid.
Im getting the date from an input text (is a masked input date) with the format '28/04/1990'.
I suspect the problem is here:
dateIsBefore(date) {
return moment(date, 'DD/MM/YYYY').isBefore(moment().format('DD/MM/YYYY'));
// --------------------------------------------------^^^^^^^^^^^^^^^^^^^^^
}
You already have a Moment (from moment()), just use it directly, don't convert it to a string:
dateIsBefore(date) {
return moment(date, 'DD/MM/YYYY').isBefore(moment());
}
That definitely makes more sense, and the reason I think it may solve your problem is that if you pass a string in to isBefore, Moment has to parse the string. Without knowing the format, faced with a string in the form ##/##/#### it's almost certainly going to use the (somewhat bizarre) U.S. format, which is MM/DD/YYYY because that's what browsers do (even outside the U.S.) when faced with that kind of string.
Passing in a Moment means you don't have to worry about that.
Its because the input parameter for the method dateIsBefore is in the format of DD-MM-YYYY this is not the valid date format.Your date format should be MM-DD-YYYY and you doesn't want to format the moment functions
Date time string format
Use this method for the date format 'DD-MM-YYYY'
function dateIsBefore(date) {
const dateInput=date.split('-') //use this line of code the input is in the format
of 'DD-MM-YYYY'
return moment(`${dateInput[1]}-${dateInput[0]}-${dateInput[2]}`).isBefore(moment());
}
Related
I am using javascript Date.parse to validate if a date string is a valid date time. I have a specific date time input like this: '8/21/2022 1:45: -04:00' when I do Date.parse('8/21/2022 1:45: -04:00'), the seconds part after : is empty, will get a number which means this date format is correct. I also tried using moment.js v2.29.2 moment('8/21/2022 1:45: -04:00').isValid(), will also get true. Just wondering if anyway to make '8/21/2022 1:45: -04:00' format to invalid? Thank you so much
I have a date which is like this "19/05/2020". I am trying to convert it into "yyyy-MM-dd" format using pipe. So the date should be look like "2020-05-19".
this._datePipe.transform("19/05/2020", 'yyyy-MM-dd');
It is giving me an error saying Unable to convert "19/04/2020" into a date' for pipe 'DatePipe
Also tried converting the string to date but again invalid date message is getting display.
Is there any way so that we can convert this date that is "19/05/2020" to a valid one so that we can perform datepipe on it.
I guess you need to convert your date having a supported formatted string :
For example, "2011-10-10" (date-only form), "2011-10-10T14:48:00" (date-time form), or "2011-10-10T14:48:00.000+09:00" (date-time form with milliseconds and time zone) can be passed and will be parsed
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
This is valid for Date.parse() or new Date().
So you'll need to parse your string with another method.
This may helps you :
How to convert dd/mm/yyyy string into JavaScript Date object?
Using HTML, javascript and jQuery I am importing a date from MS excel using "xlsx.full.min.js". I have set the date field in MS Excel to text so MS Excel does not change its format when exported. Once imported I then parse the table created to validate each field. To validate the date field I use:
var dateFormat = 'DD/MM/YYYY';
alert("$(this).text(): " + $(this).text());
alert(moment(moment($(this).text()).format(dateFormat),dateFormat,true).isValid());
When the MS Excel date field contains "13/12/2018" false is returned. When the MS Excel date field contains "12/13/2018" true is returned. Both these values are displayed in the alert so I know they are being passed correctly.
As I have set the date format to "DD/MM/YYYY" why does that format fail and "MM/DD/YYYY" pass?
MomentJS converts the date from string into its object before performing operations on it. If format is not specified, it assumes default formats. The parse operation is explained here: https://momentjs.com/docs/#/parsing/string/
So, in your case you have to pass the dateformat while moment object is created. Like this:
var dateFormat = "DD/MM/YYYY";
moment("12/13/2018",dateFormat).isValid() //will return false
moment("13/12/2018",dateFormat).isValid() //will return true
Update:
MomentJS allows date to be parsed even if the specified date contains other characters apart from the format. For eg., "1A/2B/2018" will be considered as a valid date as "1/2/2018".
Inorder to avoid that, and check the format and date to match exactly, moment object should be passed an additional boolean to enable strict mode.
moment("1A/2B/2018", dateFormat).isValid() //will return true
moment("1A/2B/2018", dateFormat, true).isValid() //will return false
There is a dynamic date format where user can specify it somewhere. I need to validate the user input (most likely via js) on a date field when he input it manually (not via datepicker).
I tried moment.js but string date like '30-01-20167' is still valid even if the date format is 'dd-MM-yyyy'
I mean is there really a reliable way to do this?
var dateFormat = "DD-MM-YYYY";
moment('30-01-20167',dateFormat, true).isValid(); // false;
moment('30-01-2016',dateFormat, true).isValid(); // true;
PS: It's better to show your code directly.
You can use javascript Date to evaluate your string:
function isValidDate(dateStr) {
return !isNaN(new Date(dateStr));
}
console.log(
isValidDate('30-01-20167'),
isValidDate('12-30-2016')
)
I'm trying to create an <input type="date"/> in Emberjs and I have two problems:
In my country the date is displayed as DD-MM-YYYY format while the date field requires a MM-DD-YYYY format (and then the browser displays it according to its locale). So the date should be formatted in one way if the browser supports the date input field and in the other way if not
The date is bound to a Date object
I'm using Momentjs for formatting and Ember Data.
I'm trying to extend Ember.TextField like this:
App.DateField = Ember.TextField.extend
value: ( (key, value) ->
if value?
if /Date/.test value.constructor #I assume that if the passed value is a Date object then it is arriving directly from the model
if Modernizr.inputtypes.date
moment(value).format('YYYY-MM-DD')
else
moment(value).format('DD-MM-YYYY')
else # if the passed value is not a Date object then the user is typing into the form
if Modernizr.inputtypes.date
value = new Date('value')
else
value
).property()
type: 'date'
For browsers with date input supports this works.
For the other browsers the date is correctly displayed, but it is saved as a (wrong formatted) string in the model.
How can I maintain the correct formatting while still using Date objects in the backend?
Demo
Update
Thanks to the blog post provided in the accepted answer I was able to update the demo to do what I want (with some weirdnesses, but not relevant at this time)
Demo2
Have look at these two blog posts:
This is a simple date picker:
http://hawkins.io/2013/06/datepicker-in-ember/
This one uses the bootstrap date picker
http://hawkins.io/2013/06/fancy-ember-datepicker-with-twitter-bootstrap/
Hopefully that will help
I don't see any parsing for the string use case. You will need to use something like Date.parse to convert the string entered by the user into a Date object.
if Modernizr.inputtypes.date
value = new Date(value)
else
value = Date.parse(value)