[英]Format the current date and time using momentjs
I want to convert the current date and time to in the following way using moment.js
. 我想使用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. 使用javascript new Date()的当前日期和时间: Thu Jul 12 2018 09:28:51 GMT+0530 (India Standard Time)
并希望将上述格式转换为以下提到的格式。
1. Thu, 12 Jul 2018 09:31:37 GMT
2. 2018-07-12T09:31:38Z
You can learn more about formatting with moment.js here. 您可以在此处了解有关使用moment.js格式化的更多信息。
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.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can use toGMTSting method to convert local time into GMT format. 您可以使用toGMTSting方法将本地时间转换为GMT格式。 Hope this helps.. 希望这可以帮助..
console.log(new Date().toGMTString()); console.log(new Date("Fri Jan 20 2012 11:51:36 GMT-0530").toGMTString());
you can also try like below for 您也可以尝试如下
- nodejs 的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);
- 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
- Html javascript 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.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can try these also: 您也可以尝试以下方法:
var moment = require('moment')
; 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. 您可以使用预先存在的本机Javascript Date对象创建Moment。
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() 您可以使用数字数组创建一个时刻,该时刻反映传递给新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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.