zoukankan      html  css  js  c++  java
  • Moment.js 超棒Javascript日期处理类库

    Moment.js 不容错过的超棒Javascript日期处理类库

    主要特性:

    1. 3.2kb超轻量级
    2. 独立类库,意味这你不需要倒入一堆js
    3. 日期处理支持UNIX 时间戳,String,指定格式的Date
    4. 日期处理:加,减日期
    5. 日期显示:包括相对时间显示的日期显示选项
    6. 其它内建的功能,例如,保存,timezone offset和i18n支持
    7. 可以作为node.js的一个模块 

    使用:

    首先,你可以创建一个新的moment 对象。它通过全局的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    
    View Code

    格式化日期

     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   
    View Code
  • 相关阅读:
    介绍Asta4D
    Mac下terminal的常用命令
    Mac下的终端(Terminal)简介
    SCA简介及配置示例
    抽象
    自助式微软BI工具PowerPivot简介!
    C编译: 使用gdb调试
    operamasksui2.0 +MVC4.0+EF5.0实战 当EntityFramework遇上Json,引爆 循环引用 这颗雷
    ASP.NET MVC分部类的使用
    GWT入门教程
  • 原文地址:https://www.cnblogs.com/kzfy/p/5053462.html
Copyright © 2011-2022 走看看