Skip to content

Instantly share code, notes, and snippets.

@xenon92
Created July 6, 2019 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenon92/4448fa7bad1f5ecc545dfa3557571179 to your computer and use it in GitHub Desktop.
Save xenon92/4448fa7bad1f5ecc545dfa3557571179 to your computer and use it in GitHub Desktop.
Simple Moment Validations (Nodejs)
"use strict"
const moment = require('moment')
class MomentValidations {
constructor(dateTimeString) {
this.dateTimeString = dateTimeString
}
isUtc() {
let formatString = 'YYYY-MM-DDTHH:mm:ssZ'
let dateObject = moment(this.dateTimeString, formatString, true)
return dateObject.isValid()
}
}
//Simple usage of a class in JS
console.log(new MomentValidations("2019-05-11T12:05:37Z").isUtc()) //--> true
//Directly using the library with the format specified
console.log(moment("2019-05-11T12:05:37Z", 'YYYY-MM-DDTHH:mm:ssZ', true).isValid()) //--> true
//Directly using the library using the internal ISO format in moment library
console.log(moment("2019-05-11T12:05:37Z").isValid()) //--> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment