How to validate wanted date time format using javascript? - javascript

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

Related

momentjs to check whether time value is present or not

How can we check with moment JS whether value has time present or not.
like, 2021-01-01 // no time. 2021-02-05 01:03:11 PM //time present.
You can parse custom date formats using Momentjs.
Your parser should be YYYY-DD-MM HH:mm:s A
Moment has a function to check if certain moment object is valid so in combination with those two things we can do something like:
if(moment('2019-02-05 01:03:11 PM', 'YYYY-DD-MM HH:mm:s A').isValid())
If date time string in correct format is provided and moment successfully initializes it's object, isValid function will return true and thus you are sure that both date and time is passed in correct format.
For more info about custom date time format strings you can check this out: https://thecodebarbarian.com/formatting-javascript-dates-with-moment-js.html
Edit: I just saw in the comments of your post that you plan on checking string length. Do not do that. It is not secure and it can (and will) produce false positives very often. The way I posted is much better because it will accept just certain format and it must be valid date.

moment js invalid date from masked input text

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());
}

Date format with Moment.js or classic JS, change places of day and month?

I get date value from my source in this format:
02.08.2016 / Day.Month.Year, it's correct.
but JavaScript understand this inverse, Month.Day.Year.
I tried some ways with Moment.js like:
moment('15.08.2016').format('MM.DD.YYYY');
My JsFiddle is: http://jsfiddle.net/PAc3j/389/
There are some results visible, see please output, variable dateThree: Invalid date
My question is, how can I do it with Moment.js or classic JavaScript?
You can specify the format of the input date string like this.
moment('15.08.2016', 'DD.MM.YYYY').format('MM.DD.YYYY');
http://momentjs.com/docs/#/parsing/string-format

Angularjs date display

Hi I am trying to figure out how to display date properly using angularjs.
My system returns the date in this fromat
c_date="\/Date(1151470800000-0500)\/"
I dont know what format is it.
I am displaying it
using <span>{{pro.c_date | date:'medium')</span>
When i run this i get date printed in same format
"/Date(1151470800000-0500)/"
Can anyone suggest how to fix this so date can be displayed properly.
Thanks
the date that you're trying to use is a invalid date or maybe I don't know what format are the system using.
The problem is that angular.js requires a data object for can format it. Not a date string.
I suppose that the date is 1151470800000 miliseconds since the epoch least 0500 for adjust hours or something.
If this is correct, you only need to make c_date a Date object before pass to view.
c_date = "\/Date(1151470800000-0500)\/";
var the_date = eval("new "+c_date.replace(/\\|\//g, ''));
http://jsbin.com/zuwizoxe/3/
Regards!

Convert date to unix timestamp through jQuery datepicker

I want to check whether the entered dob is above 18th old. For that i want to convert to convert the entered date to the unix time stamp. I tried using:
$.datepicker.formatDate('#', $('#dob').val());
But it gives three additional zeros and also goes one day earlier. Like for "10/10/1967" it returns: -70369200000 while it should return: -70329600. How should i change it?
To get the Unix time out of a date picker use:
$(el).datepicker('getDate') / 1000;
However you should also note that the semantics of Date objects are not well defined for "negative" dates.

Categories