How to use the isDate function from validator

Find comprehensive JavaScript validator.isDate code examples handpicked from public code repositorys.

validator.isDate is a function that checks if a given input is a valid date or not.

120
121
122
123
124
125
126
127
128
129
},
timestamp: function(x) {
  check(x, "a positive integer", validator.isInt(x, 0));
},
date: function(x) {
  check(x, "a date", validator.isDate(x));
},
isbn: function(x) {
  check(x, "a book number", validator.isISBN(x));
},
fork icon2
star icon4
watch icon0

219
220
221
222
223
224
225
226
227
228
case 'bool':
  isValid = validator.isBoolean(stringValue);
  break;

case 'date':
  isValid = validator.isDate(stringValue);
  break;

case 'email':
  isValid = validator.isEmail(stringValue);
fork icon0
star icon2
watch icon1

+ 31 other calls in file

How does validator.isDate work?

validator.isDate is a function that determines whether a given value is a valid date by checking if it is a valid date string that can be parsed by the JavaScript Date object or if it is a valid Date instance. The function also allows for the validation of custom date formats by accepting a second parameter specifying the format.

128
129
130
131
132
133
134
135
136
137
  }
  return true;
},
isDate: function(rule, value) {
  if (rule) {
    return validator.isDate(value);
  }
  return true;
},
isAfter: function(rule, value) {
fork icon0
star icon2
watch icon2

+ 5 other calls in file

155
156
157
158
159
160
161
162
163
164
// TODO: write more tests (from isDate to isURL)
validations.isDate = function isDate(paramName, customMessage) {
  return checkParam(
    paramName,
    customMessage || message(paramName, 'should be a date'),
    validator.isDate
  );
}

validations.isDecimal = function isDecimal(paramName, customMessage) {
fork icon9
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
const validator = require("validator");

const dateString = "2023-04-03";
const isValid = validator.isDate(dateString);

console.log(isValid); // true

In this example, the validator module is first imported. A date string dateString is then defined as '2023-04-03'. The validator.isDate method is used to check if the string is a valid date. The result is then logged to the console, which will print true.

129
130
131
132
133
134
135
136
137
138
  parseInt(datArray[2]),
  parseInt(datArray[1]),
  parseInt(datArray[0])
);
if (
  validator.isDate(datedeb) == false ||
  validator.isDate(datefin) == false
) {
  messageJson.msg = "L'une des dates est incorrect.";
  return response.json(messageJson);
fork icon0
star icon0
watch icon1

21
22
23
24
25
26
27
28
29
30
31
32
33
}


// Validate date


exports.validateDate = (date) => {
    return validator.isDate(date);
}


// Validate URL
exports.validateURL = (url) => {
fork icon0
star icon0
watch icon0

87
88
89
90
91
92
93
94
95

let is_full_name_OK = validator.isAlpha(full_name, ['pt-BR'], {
  ignore: acceptableCharacters.inName
})
let is_email_OK = validator.isEmail(email)
let is_born_date_OK = validator.isDate(born_date)
let is_user_password_OK = validator.isAlphanumeric(user_password, ['pt-BR'], {
  ignore: acceptableCharacters.inPassword
})
fork icon0
star icon0
watch icon0

+ 3 other calls in file