标签:
Moment.js 不容错过的超棒Javascript日期处理类库
主要特性:
使用:
首先,你可以创建一个新的moment 对象。它通过全局的moment() 方法来完成。如果你不设置参数,它将使用当前的时间。
1 // Create a new moment object 2 var now = moment(); 3 4 // Create a moment in the past, using a string date 5 var m = moment("April 1st, 2005", "MMM-DD-YYYY"); 6 7 // Create a new moment using an array 8 var m = moment([2005, 3, 1]);
注意:和JavaScript Date() 对象一样,月份是从0开始的,所以3是4月份。
1 // What time is it? 2 console.log(moment().format(‘HH:mm:ss‘)); // 16:13:11 3 4 // What day of the week is it? 5 var day = moment().day(); // 5 6 console.log( moment.weekdays[day] ); // Friday 7 8 // What is the current month name? 9 console.log( moment.months[moment().month()] ); // August 10 11 // What time is it in London? (time zone: UTC) 12 console.log( moment.utc().format(‘HH:mm:ss‘) ); // 13:23:41 13 14 // What time is it in Japan? (time zone: UTC+9) 15 console.log( moment.utc().add(‘hours‘,9).format(‘HH:mm:ss‘) ); // 22:23:41
1 // How old are you? 2 var m = moment("Mar 26th, 1989", "MMM-DD-YYYY"); 3 4 console.log(‘You are ‘+m.fromNow() + ‘ old‘); // You are 23 years ago old 5 6 // Oops. We better leave the "ago" part out: 7 console.log(‘You are ‘+m.fromNow(true) + ‘ old‘); // You are 23 years old 8 9 // When will the next world cup be? 10 console.log( moment(‘June 12th, 2014‘,‘MMM DD YYYY‘).fromNow() ); // in 2 years 11 12 // What will be the date 7 days from now? 13 console.log( moment().add(‘days‘,7).format(‘MMMM Do, YYYY‘) ); // September 7th, 2012
标签:
原文地址:http://www.cnblogs.com/kzfy/p/5053462.html