最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Format the current date and time using momentjs - Stack Overflow

programmeradmin1浏览0评论

I want to convert the current date and time to in the following way using moment.js.

Current date and time using javascript new Date(): Thu Jul 12 2018 09:28:51 GMT+0530 (India Standard Time) and want to convert the above format to below mentioned format.

 1. Thu, 12 Jul 2018 09:31:37 GMT

 2. 2018-07-12T09:31:38Z

I want to convert the current date and time to in the following way using moment.js.

Current date and time using javascript new Date(): Thu Jul 12 2018 09:28:51 GMT+0530 (India Standard Time) and want to convert the above format to below mentioned format.

 1. Thu, 12 Jul 2018 09:31:37 GMT

 2. 2018-07-12T09:31:38Z
Share Improve this question asked Jul 12, 2018 at 4:05 vishnuvishnu 4,59916 gold badges62 silver badges92 bronze badges 2
  • 1 Why not use moment().toISOString()? – Andrew Li Commented Jul 12, 2018 at 4:09
  • moment(new Date()).utc().format(); – Neha Commented Jul 12, 2018 at 4:50
Add a ment  | 

4 Answers 4

Reset to default 5

You can learn more about formatting with moment.js here.

Escape words in formatting with escaping-characters "[]".

console.log(moment());
console.log(moment().format('ddd, DD MMM YYYY HH:mm:ss [GMT]'));
console.log(moment().format('YYYY-MM-DD[T]HH:mm:ss[Z]'));
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.22.2/moment.min.js"></script>

you can also try like below for

  1. nodejs
var moment = require('moment');
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();

console.log(format1);
console.log(format2);
  1. angular
import moment from 'moment';
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();

console.log(format1);
console.log(format2);

Install moment like below

npm install --save moment
  1. Html javascript
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')

var format2 = moment().toDate();

console.log(format1);

console.log(format2);

moment source script

<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.22.2/moment.min.js"></script>

You can use toGMTSting method to convert local time into GMT format. Hope this helps..

console.log(new Date().toGMTString());
console.log(new Date("Fri Jan 20 2012 11:51:36 GMT-0530").toGMTString());

You can try these also:

var moment = require('moment');

let startDate= new moment('11/22/1990','MM/DD/YYYY').format("YYYY/MM/DD");

You can also understand these:

let otherDate= 1399919400000;
var neweDate = moment(otherDate).format('DD/MM/YYYY');
//My neweDate output is "13/05/2014";

moment.locale('cs');
console.log(moment.locale()); // en

moment("2010-10-20 4:30",       "YYYY-MM-DD HH:mm");   // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC

You can verify the date also:

moment("not a real date").isValid(); // false
moment("2010 13",           "YYYY MM").isValid();     // false (not a real month)
moment("2010 11 31",        "YYYY MM DD").isValid();  // false (not a real day)
moment("2010 2 29",         "YYYY MM DD").isValid();  // false (not a leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)

You can create a Moment with a pre-existing native Javascript Date object.

var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);

You can create a moment with an array of numbers that mirror the parameters passed to new Date()

[year, month, day, hour, minute, second, millisecond]

moment([2010, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM

Any value past the year is optional, and will default to the lowest possible number.

moment([2010]);        // January 1st
moment([2010, 6]);     // July 1st
moment([2010, 6, 10]); // July 10th
发布评论

评论列表(0)

  1. 暂无评论