const format1 = "YYYY-MM-DD HH:mm:ss"
const format2 = "YYYY-MM-DD"
var date1 = new Date("2020-06-24 22:57:36");
var date2 = new Date();
dateTime1 = moment(date1).format(format1);
dateTime2 = moment(date2).format(format2);
document.getElementById("demo1").innerHTML = dateTime1;
document.getElementById("demo2").innerHTML = dateTime2;
<!DOCTYPE html>
<html>
<body>
<p id="demo1"></p>
<p id="demo2"></p>
<script src="https://momentjs.com/downloads/moment.js"></script>
</body>
</html>
Answer from Jayram on Stack Overflowconst format1 = "YYYY-MM-DD HH:mm:ss"
const format2 = "YYYY-MM-DD"
var date1 = new Date("2020-06-24 22:57:36");
var date2 = new Date();
dateTime1 = moment(date1).format(format1);
dateTime2 = moment(date2).format(format2);
document.getElementById("demo1").innerHTML = dateTime1;
document.getElementById("demo2").innerHTML = dateTime2;
<!DOCTYPE html>
<html>
<body>
<p id="demo1"></p>
<p id="demo2"></p>
<script src="https://momentjs.com/downloads/moment.js"></script>
</body>
</html>
Use different format or pattern to get the information from the date
var myDate = new Date("2015-06-17 14:24:36");
console.log(moment(myDate).format("YYYY-MM-DD HH:mm:ss"));
console.log("Date: "+moment(myDate).format("YYYY-MM-DD"));
console.log("Year: "+moment(myDate).format("YYYY"));
console.log("Month: "+moment(myDate).format("MM"));
console.log("Month: "+moment(myDate).format("MMMM"));
console.log("Day: "+moment(myDate).format("DD"));
console.log("Day: "+moment(myDate).format("dddd"));
console.log("Time: "+moment(myDate).format("HH:mm")); // Time in24 hour format
console.log("Time: "+moment(myDate).format("hh:mm A"));
<script src="https://momentjs.com/downloads/moment.js"></script>
For more info: https://momentjs.com/docs/#/parsing/string-format/
You need to call format() function to get the formatted value
$scope.SearchDate = moment(new Date()).format("DD/MM/YYYY")
//or $scope.SearchDate = moment().format("DD/MM/YYYY")
The syntax you have used is used to parse a given string to date object by using the specified formate
You can use this
moment().format("DD/MM/YYYY");
However, this returns a date string in the specified format for today, not a moment date object. Doing the following will make it a moment date object in the format you want.
var someDateString = moment().format("DD/MM/YYYY");
var someDate = moment(someDateString, "DD/MM/YYYY");
If you don't see it in the documentation (and I don't either), that suggests that no, there is no current preset for that in Moment.
You could always add your own:
Object.defineProperty(moment.fn, "myYMD", { // or moment.prototype, they refer to
// the same object according to the docs
value: function() {
return this.format("YYYY-MM-DD");
},
configurable: true,
writable: true
});
(Using Object.defineProperty because I never add enumerable properties to prototypes.)
Note the prefix on the method name, to avoid conflicts with future extensions to Moment.
Example:
Object.defineProperty(moment.fn, "myYMD", {
value: function() {
return this.format("YYYY-MM-DD");
},
configurable: true,
writable: true
});
// using it
console.log(moment().myYMD());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Or just use a utility method you pass the Moment instance into.
As version 2.20.0, you can use Special Formats (even if they are mentioned in the parsing section of the docs), for example you can use moment.HTML5_FMT.DATE to get output in YYYY-MM-DD format:
console.log(moment().format(moment.HTML5_FMT.DATE));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
If you want, you can also customize default format, to make format() (without parameter) display result in your preferred format:
console.log(moment().format());
moment.defaultFormat = 'YYYY-MM-DD';
console.log(moment().format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Or if you want you can extend moment using moment.fn and build a function that accepts your tokens (like 'C' and 'I'):
moment.fn.myFormat = function(token){
if( token == 'C' ){
return this.format('YYYY-MM-DD');
} else if( token == 'I' ){
return this.toISOString();
} else {
return this.format();
}
}
console.log(moment().myFormat());
console.log(moment().myFormat('C'));
console.log(moment().myFormat('I'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
The
Momentprototype is exposed throughmoment.fn. If you want to add your own functions, that is where you would put them.
StartDate = moment(StartDate).format('MM-YYYY');
...and MySQL date format:
StartDate = moment(StartDate).format('YYYY-MM-DD');
I think you just have incorrect casing in the format string. According to the documentation this should work for you: MM/DD/YYYY
moment.js documentation