Format Date with Moment.Js

Format date with Moment.js

The 2nd argument to moment() is a parsing format rather than an display format.

For that, you want the .format() method:

moment(testDate).format('MM/DD/YYYY');

Also note that case does matter. For Month, Day of Month, and Year, the format should be uppercase.

Format datetime to YYYY-MM-DD HH:mm:ss in moment.js

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>

Format existing date variable with moment.js

You're using moment(String) method, but you're not passing a Supported
format that it expects to parse.

You should use moment(String, String), where the first String is the input date string, and second is the format of your input date String.

Try this:

moment(date+' '+time,'DD/MM/YYYY HH:mm').format('MM.DD.YYYY');

DD/MM/YYYY Date format in Moment.js

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

momentjs datetime format on frontend with angular

moment().format('YYYY-MM-DD HH:mm:ss') will give you the format you want but since you are using date_times as the ngModel of <ion-datetime> component, its value has been changed after you initialized the value in the constructor().

You can format date_times when you print it out by using Pipe like this:

my-datetime-format.pipe.ts:

import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';

@Pipe({
name: 'myDateTimeFormat'
})
export class myDateTimeFormatPipe implements PipeTransform {
transform(value: string): string {
return moment(value).format('YYYY-MM-DD HH:mm:ss');
}
}

In template:

{{ date_times | myDateTimeFormat }}

How to format a date in Moment.js

From http://momentjs.com/docs/#/displaying/format/ we can see that "LLL" represents the format "Month name, day of month, year, time". It seems you want "Month day, year", which is "LL".

Try:

picker.format = 'LL';
formatted = moment(picker.date).format(picker.format);
console.log(formatted);

Outputs (with today's date):

September 15, 2016

moment.js parse date with strict format and locale

The issue is that you are not properly importing the localization, have a look at Loading locales in NodeJS section in the docs.

The linked code works if you put:

import moment from "moment/min/moment-with-locales";

instead of

import moment from "moment";

Moment js: format date without zero padding

According to the docs (https://momentjs.com/docs/#/parsing/string-format/) if you use M or D instead of MM or DD in the format() function you will get the date without the 0.

moment().format("YYYY-M-DD") is what you are after.

if you also want to exclude the 0 from single digit days you can use:

moment().format("YYYY-M-D")

(fiddle here: http://jsfiddle.net/rLjQx/69671/)



Related Topics



Leave a reply



Submit