Định dạng mili giây python

Khoảng khăc. js đã được sử dụng thành công trong hàng triệu dự án và chúng tôi rất vui vì đã góp phần làm cho ngày và giờ tốt hơn trên web. Tính đến tháng 9 năm 2020, Moment nhận được hơn 12 triệu lượt tải xuống mỗi tuần. Tuy nhiên, Moment được xây dựng cho kỷ nguyên trước của hệ sinh thái JavaScript. Web hiện đại ngày nay trông khác nhiều. Moment đã phát triển phần nào trong những năm qua, nhưng về cơ bản nó vẫn giữ nguyên thiết kế như khi nó được tạo ra vào năm 2011. Dựa vào số lượng dự án phụ thuộc vào nó, chúng tôi chọn ưu tiên tính ổn định hơn các tính năng mới

Ví dụ, xem xét rằng các đối tượng Khoảnh khắc có thể thay đổi. Đây là một nguồn khiếu nại phổ biến về Moment. Chúng tôi giải quyết vấn đề này nhưng nó vẫn gây ngạc nhiên cho hầu hết người dùng mới. Thay đổi Khoảnh khắc thành bất biến sẽ là một thay đổi đột phá đối với mọi dự án sử dụng nó. Tạo một "Moment v3" không thay đổi sẽ là một công việc to lớn và sẽ biến Moment thành một thư viện hoàn toàn khác. Vì điều này đã được thực hiện trong các thư viện khác, chúng tôi cảm thấy rằng điều quan trọng hơn là giữ lại API có thể thay đổi

Một lập luận phổ biến khác chống lại việc sử dụng Moment trong các ứng dụng hiện đại là kích thước của nó. Khoảnh khắc không hoạt động tốt với các thuật toán "rung cây" hiện đại, do đó, nó có xu hướng tăng kích thước của gói ứng dụng web. Nếu một người cần hỗ trợ quốc tế hóa hoặc múi giờ, Moment có thể khá lớn. Các trình duyệt web hiện đại (và Node. js) hiển thị hỗ trợ quốc tế hóa và múi giờ thông qua đối tượng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
68, được mã hóa thành ECMA-402. Các thư viện như Luxon (và các thư viện khác) tận dụng lợi thế này, giảm hoặc loại bỏ nhu cầu gửi các tệp dữ liệu của riêng bạn

Gần đây, Chrome Dev Tools đã bắt đầu hiển thị các đề xuất để thay thế Khoảnh khắc cho riêng kích thước. Chúng tôi thường ủng hộ động thái này

Bạn cũng có thể muốn đọc

  • Có lẽ bạn không cần thời gian. js nữa
  • Bạn không (có thể không) cần Khoảnh khắc. js
  • Tại sao bạn không nên sử dụng Moment. js
  • 4 lựa chọn thay thế cho thời điểm. js để quốc tế hóa ngày tháng

Nhóm Moment đã thảo luận về những vấn đề này rất lâu. Chúng tôi nhận thấy rằng nhiều dự án hiện tại có thể tiếp tục sử dụng Moment, nhưng chúng tôi không muốn Moment được sử dụng trong các dự án mới trong tương lai. Thay vào đó, chúng tôi muốn đó là sự lựa chọn tuyệt vời để sử dụng trong các ứng dụng hiện đại ngày nay. Chúng tôi cũng muốn thúc đẩy việc bổ sung ngôn ngữ JavaScript đang tìm kiếm phản hồi và cộng tác viên

Hiện tại, chúng tôi thường coi Khoảnh khắc là một dự án kế thừa ở chế độ bảo trì. Nó không chết, nhưng nó thực sự được thực hiện

Trong thực tế, điều này có nghĩa là

  • Chúng tôi sẽ không thêm các tính năng hoặc khả năng mới
  • Chúng tôi sẽ không thay đổi API của Moment thành bất biến
  • Chúng tôi sẽ không giải quyết các vấn đề về rung cây hoặc kích thước bó
  • Chúng tôi sẽ không thực hiện bất kỳ thay đổi lớn nào (không có phiên bản 3)
  • Chúng tôi có thể chọn không sửa lỗi hoặc hành vi kỳ quặc, đặc biệt nếu chúng là những vấn đề đã biết từ lâu

Liên quan cụ thể đến các tệp ngôn ngữ quốc tế hóa của Moment

  • Chúng tôi có thể chọn không chấp nhận các sửa đổi đối với chuỗi bản địa hóa hoặc định dạng ngày được bản địa hóa, đặc biệt nếu chúng đã được lập luận thành công cho biểu mẫu hiện tại của chúng
  • Bạn phải đưa ra một lập luận thuyết phục mới cho những thay đổi ngôn ngữ với bằng chứng quan trọng, phi giai thoại để hỗ trợ cho quan điểm của bạn
  • Nếu chuỗi hoặc định dạng bạn đang yêu cầu thay đổi được phản ánh trong CLDR, thì bạn phải gửi thay đổi ở đó trước và được chấp nhận

Tuy nhiên, vì chúng tôi hiểu rằng Khoảnh khắc đã được thiết lập tốt trong hàng triệu dự án hiện có

  • Chúng tôi sẽ giải quyết các vấn đề quan trọng về bảo mật khi chúng phát sinh
  • Chúng tôi sẽ phát hành các bản cập nhật dữ liệu cho Múi giờ theo thời điểm sau khi phát hành cơ sở dữ liệu múi giờ IANA

Lý do để tiếp tục sử dụng Moment

Trong hầu hết các trường hợp, bạn không nên chọn Moment cho các dự án mới. Tuy nhiên, có một số lý do có thể khiến bạn muốn tiếp tục sử dụng nó

hỗ trợ trình duyệt

Khoảnh khắc hoạt động tốt trên Internet Explorer 8 trở lên. Ngược lại, Luxon chỉ hoạt động trên IE 10 trở lên và yêu cầu một polyfill để làm như vậy.

Các thư viện khác cũng gặp sự cố với Safari, đặc biệt là trên thiết bị di động. Nếu bạn có yêu cầu mạnh mẽ để hỗ trợ các trình duyệt cũ hơn, thì bạn có thể muốn gắn bó với Moment lâu hơn một chút

Tuy nhiên, ngày. js báo cáo khả năng tương thích với IE8 trở lên, do đó bạn vẫn có thể muốn xem xét giải pháp thay thế đó

Sự phụ thuộc của các thư viện khác

Một số thư viện khác, đặc biệt là bộ chọn ngày và thư viện vẽ đồ thị, lấy Khoảnh khắc làm phụ thuộc. Nếu bạn đang sử dụng một thành phần như vậy và không thể tìm thấy một thành phần thay thế, thì bạn đã bao gồm Khoảnh khắc trong dự án của mình. Do đó, có thể hợp lý khi tiếp tục sử dụng Khoảnh khắc trong suốt dự án của bạn thay vì bao gồm một thư viện ngày và giờ khác

quen thuộc

Nếu bạn là người dùng Moment lâu năm, bạn có thể đã hiểu rõ về API và các hạn chế của nó. Nếu đúng như vậy và các vấn đề nêu trên không phải là mối bận tâm của bạn, thì bạn chắc chắn có thể tiếp tục sử dụng nó

chỉnh sửa

Có một số tùy chọn tuyệt vời để xem xét sử dụng thay vì Khoảnh khắc

Khi lựa chọn, hãy xem xét rằng

  • Một số thư viện được chia thành các mô-đun, plugin hoặc thư viện đồng hành
  • Một số thư viện sử dụng API ECMAScript
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    68 cho ngôn ngữ, múi giờ hoặc cả hai
  • Một số thư viện vẫn cung cấp các tệp miền địa phương và múi giờ của riêng họ như Moment và Moment-Timezone do

Dưới đây là các lựa chọn thay thế chúng tôi đề xuất

Luxon

Luxon có thể được coi là sự phát triển của Moment. Nó được tác giả bởi Isaac Cambron, một cộng tác viên lâu năm của Moment. Vui lòng đọc và các trang trong tài liệu Luxon

  • địa phương.
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    68 được cung cấp
  • Múi giờ.
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    68 được cung cấp

Ngày. js

Ngày. js được thiết kế để thay thế tối giản cho Moment. js, sử dụng API tương tự. Đây không phải là sự thay thế tùy chọn, nhưng nếu bạn đã quen sử dụng API của Moment và muốn di chuyển nhanh chóng, hãy cân nhắc sử dụng Day. js

  • địa phương. Các tệp dữ liệu tùy chỉnh có thể được nhập riêng lẻ
  • Múi giờ.
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    68 được cung cấp, thông qua plugin

ngày-fns

Date-fns cung cấp một loạt hàm để thao tác với các đối tượng JavaScript

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
74. Để biết thêm chi tiết, hãy cuộn đến "Tại sao lại là date-fns?"

  • địa phương. Các tệp dữ liệu tùy chỉnh có thể được nhập riêng lẻ
  • Múi giờ.
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    68 được cung cấp, thông qua một thư viện đồng hành riêng biệt

js-Joda

js-Joda là một cổng JavaScript của Three-Ten Backport của Java, là cơ sở để triển khai JSR-310 của gói Java SE 8

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
76. Nếu bạn đã quen thuộc với
// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
76, Joda-Time hoặc Noda Time, bạn sẽ thấy js-Joda có thể so sánh được

  • địa phương. Các tệp dữ liệu tùy chỉnh thông qua mô-đun bổ trợ
  • Múi giờ. Các tệp dữ liệu tùy chỉnh thông qua mô-đun bổ trợ

Không có thư viện

JavaScript luôn có một đối tượng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
74, đặc tả ECMAScript (ECMA-262) đã xác định

Khi sử dụng các đối tượng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
74, hãy lưu ý những điều sau

  • Đối tượng

    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    74 bên trong đại diện cho dấu thời gian Unix với độ chính xác đến mili giây. Nó cung cấp các chức năng sẽ chuyển đổi sang và từ múi giờ địa phương của hệ thống, nhưng bên trong nó luôn là UTC. Không giống như một đối tượng
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    81, nó không thể được đặt để sử dụng múi giờ khác;

  • Sử dụng

    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    82 hoặc đã có vấn đề và được triển khai không nhất quán trong quá khứ. Định nghĩa phân tích cú pháp một biến thể của các chuỗi ISO 8601, trong đó các biểu mẫu chỉ có ngày (như
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    84) được phân tích cú pháp thành UTC, thay vì giờ địa phương như theo tiêu chuẩn ISO 8601. Ngay cả khi đó, không phải tất cả các triển khai hiện đại đều triển khai chính xác đặc điểm kỹ thuật này (e. g. , Cuộc đi săn). Các loại chuỗi khác có thể hoạt động, nhưng việc phân tích cú pháp chúng là triển khai cụ thể và có thể thay đổi đáng kể - đặc biệt là với các trình duyệt cũ hơn. Tùy thuộc vào cách triển khai và các thành phần được cung cấp trong chuỗi, bạn có thể ngạc nhiên với kết quả. Vì những lý do này, chúng tôi đồng ý với việc phân tích chuỗi với đối tượng
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    74 không được khuyến khích

Các môi trường JavaScript hiện đại cũng sẽ triển khai đặc tả ECMA-402, cung cấp đối tượng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
68 và xác định các tùy chọn hành vi của các hàm
// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
88, ________0__89 và ________0____90 của đối tượng
// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
74

Khi sử dụng đối tượng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
68, hãy lưu ý những điều sau

  • Không phải mọi môi trường sẽ triển khai thông số kỹ thuật đầy đủ. Đặc biệt, nút. js yêu cầu hỗ trợ quốc tế hóa do ICU cung cấp. Xem nút. js để biết thêm chi tiết
  • Bảng tương thích ECMAScript Intl (của kangax) có thể hữu ích trong việc xác định tính năng nào được hỗ trợ và tính năng nào không
  • Hầu hết các môi trường mới hơn đều cung cấp hỗ trợ múi giờ IANA thông qua tùy chọn
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    92 trong hàm tạo
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    93 (và trong
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    94, ________0____95 và
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    96). Tùy chọn này có thể được sử dụng để lấy dấu thời gian dựa trên UTC nội bộ của đối tượng
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    74 và nhận chuỗi đã được chuyển đổi thành múi giờ được đặt tên. Tuy nhiên, nó không thể được sử dụng để chuyển đổi đối tượng
    // only needing core
    define(['moment'], function (moment) {
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    });
    
    // core with single locale
    define(['moment', 'moment/locale/de'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // core with all locales
    define(['moment/min/moment-with-locales'], function (moment) {
        moment.locale('de');
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
    });
    
    // async load locale
    define(['require', 'moment'], function(require, moment) {
      // Inside some module after the locale is detected. This is the
      // case where the locale is not known before module load time.
      require(['moment/locale/de'], function(localeModule) {
        // here the locale is loaded, but not yet in use
        console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
    
        moment.locale('de');
        // Use moment now that the locale has been properly set.
        console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
      })
    });
    
    74 sang múi giờ khác

Nếu các đối tượng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
74 và
// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
68 đáp ứng nhu cầu của bạn và bạn hoàn toàn hiểu những hạn chế của chúng, thì bạn có thể cân nhắc sử dụng chúng trực tiếp

chỉnh sửa

Tạm thời - Ngày và giờ tốt hơn bằng ngôn ngữ JavaScript

Một ngày không xa, chúng tôi hy vọng sẽ không còn nhu cầu lớn về thư viện ngày và giờ trong JavaScript nữa. Thay vào đó, chúng tôi sẽ có thể sử dụng các khả năng của chính ngôn ngữ JavaScript. Mặc dù ngày nay một số khả năng đã có ở đây với

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
74 và
// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
68, nhưng từ kinh nghiệm và dữ liệu, chúng tôi biết rằng vẫn còn chỗ đáng kể để cải thiện

Nỗ lực tạo API ngày và giờ tốt hơn bằng ngôn ngữ JavaScript đang được thực hiện thông qua Đề xuất tạm thời ECMA TC39. Nó hiện đang ở Giai đoạn 3 của quy trình TC39

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
69 sẽ là một đối tượng toàn cầu mới hoạt động như một không gian tên cấp cao nhất (như
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
04). Nó hiển thị nhiều loại đối tượng riêng biệt bao gồm
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
05,
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
06,
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
07,
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
08,
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
09,
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
10 và một số đối tượng khác. Sách dạy nấu ăn tạm thời hiển thị nhiều "công thức nấu ăn" với các ví dụ về cách sử dụng các đồ vật này trong các tình huống khác nhau

Bạn có thể dùng thử Temporal ngay hôm nay, thông qua một polyfill phi sản xuất. Vui lòng dùng thử, nhưng đừng sử dụng nó trong sản xuất (chưa)

Vui lòng cung cấp phản hồi và cân nhắc đóng góp cho nỗ lực này - đặc biệt nếu bạn có kinh nghiệm sử dụng Khoảnh khắc hoặc các thư viện ngày và giờ khác

chỉnh sửa

Chúng tôi thực sự khuyên bạn nên đọc nếu bạn định sử dụng khoảnh khắc với Yêu cầu. js. Cũng nâng cấp lên 2. 14. 0 trở lên để có trải nghiệm tốt nhất

Khi bắt đầu, bạn có thể có được khoảnh khắc thông qua bower hoặc node_modules hoặc bất kỳ thứ gì khác đặt khoảnh khắc. js cùng với thư mục ngôn ngữ trong thư mục cơ sở. Sau đó, bạn nên sử dụng một công cụ như Adapt-pkg-main hoặc thủ công -- sử dụng

requirejs.config({
  packages: [{
    name: 'moment',
    // This location is relative to baseUrl. Choose bower_components
    // or node_modules, depending on how moment was installed.
    location: '[bower_components|node_modules]/moment',
    main: 'moment'
  }]
});

Với cách thiết lập ở trên, bạn có thể yêu cầu lõi có ngôn ngữ

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
11 và
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
12 với ngôn ngữ
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
13

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});

Đối với các trường hợp sử dụng phức tạp hơn, vui lòng đọc

Khoảnh khắc vẫn sẽ tạo ra một

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
11 toàn cầu, hữu ích cho các plugin và mã của bên thứ ba khác. Nếu bạn muốn xóa toàn cầu đó, hãy sử dụng tùy chọn
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
15 trên cấu hình mô-đun

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});

Nếu bạn không chỉ định

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
15 thì thời điểm được xuất trên toàn cầu sẽ in cảnh báo không dùng nữa. Từ bản phát hành chính tiếp theo, bạn sẽ phải tự xuất nó nếu muốn hành vi đó

Đối với phiên bản 2. 5. x, trong trường hợp bạn sử dụng các plugin khác dựa trên Moment nhưng không tương thích với AMD, bạn có thể cần thêm vào r. cấu hình js

Ghi chú. Để cho phép khoảnh khắc. js sẽ được tải trong môi trường requestjs, thời điểm được tạo dưới dạng mô-đun có tên. Do đó, thời điểm phải được tải chính xác như

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
18, sử dụng
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
19 để xác định thư mục. Thời điểm yêu cầu có đường dẫn như
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
20 sẽ trả về
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
21

Ghi chú. Từ phiên bản 2. 9. 0 tự xuất dưới dạng mô-đun ẩn danh, vì vậy nếu bạn chỉ sử dụng lõi (không có ngôn ngữ / plugin), thì bạn không cần cấu hình nếu đặt nó ở vị trí không chuẩn

1. 0. 0+

chỉnh sửa

moment(String);

Khi tạo một khoảnh khắc từ một chuỗi, trước tiên chúng tôi kiểm tra xem chuỗi đó có khớp với các định dạng ISO 8601 đã biết hay không, sau đó chúng tôi kiểm tra xem chuỗi đó có khớp với định dạng hay không trước khi thả xuống phần sau của

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
22 nếu không tìm thấy định dạng đã biết

var day = moment("1995-12-25");

Cảnh báo. Hỗ trợ trình duyệt để phân tích chuỗi không nhất quán. Do không có thông số kỹ thuật về định dạng nào sẽ được hỗ trợ nên những gì hoạt động trong một số trình duyệt sẽ không hoạt động trong các trình duyệt khác

Để có kết quả nhất quán khi phân tích cú pháp bất kỳ thứ gì khác ngoài chuỗi ISO 8601, bạn nên sử dụng

Chuỗi ISO 8601 được hỗ trợ

Một chuỗi ISO 8601 yêu cầu một phần ngày

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)

Phần thời gian cũng có thể được bao gồm, được phân tách với phần ngày bằng dấu cách hoặc chữ T viết hoa

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only

Bất kỳ phần ngày nào cũng có thể có phần thời gian

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part

Nếu bao gồm một phần thời gian, thì phần bù từ UTC cũng có thể được bao gồm như

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
23,
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
24,
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
25 hoặc
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
26

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH

Ghi chú. Hỗ trợ cho các định dạng tuần và thứ tự đã được thêm vào trong phiên bản 2. 3. 0

Nếu một chuỗi không khớp với bất kỳ định dạng nào ở trên và không thể phân tích cú pháp bằng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
82, thì
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
28 sẽ trả về false

moment("not a real date").isValid(); // false

Định dạng ngày giờ RFC 2822

Trước khi phân tích cú pháp ngày giờ RFC 2822, chuỗi được xóa để xóa mọi nhận xét và/hoặc ký tự dòng mới. Các ký tự bổ sung có định dạng hợp pháp nhưng không bổ sung gì để tạo một phiên bản thời điểm hợp lệ

Sau khi làm sạch, chuỗi được xác thực trong các phần được phân tách bằng dấu cách sau đây, tất cả đều sử dụng ngôn ngữ tiếng Anh

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
0
  1. Ngày trong tuần bằng ba chữ cái, theo sau là dấu phẩy tùy chọn. (không bắt buộc)
  2. Ngày trong tháng (1 hoặc 2 chữ số), theo sau là tháng gồm ba chữ cái và năm gồm 2 hoặc 4 chữ số
  3. Hai chữ số giờ và phút cách nhau bởi dấu hai chấm (. ), theo sau tùy ý là dấu hai chấm khác và giây ở dạng 2 chữ số
  4. Múi giờ hoặc độ lệch ở một trong các định dạng sau
  5. UT. +0000
  6. giờ GMT. +0000
  7. EST. CST. MST. PST. EDT. CĐT. MDT. PDT. Múi giờ Hoa Kỳ*
  8. Một - tôi. K-Z. Múi giờ quân sự*
  9. Thời gian bù +/-9999

[*] Xem thông số kỹ thuật để biết chi tiết

Trình phân tích cú pháp cũng xác nhận rằng ngày trong tuần (khi được đưa vào) nhất quán với ngày

1. 0. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
1

Nếu bạn biết định dạng của chuỗi đầu vào, bạn có thể sử dụng định dạng đó để phân tích một khoảnh khắc

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
2

Theo mặc định, trình phân tích cú pháp bỏ qua các ký tự không phải chữ và số, vì vậy cả hai ký tự sau đây sẽ trả về cùng một thứ

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
3

Bạn có thể nhận được kết quả không mong muốn khi phân tích cả ngày và giờ. Ví dụ dưới đây có thể không phân tích cú pháp như bạn mong đợi

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
4

Bạn có thể sử dụng chế độ nghiêm ngặt, chế độ này sẽ xác định lỗi phân tích cú pháp và đặt đối tượng Khoảnh khắc là không hợp lệ

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
5

Mã thông báo phân tích cú pháp tương tự như mã thông báo định dạng được sử dụng trong

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
29

Mã thông báo năm, tháng và ngày

Mã thông báo phân biệt chữ hoa chữ thường

Đầu vàoVí dụMô tả_______34_______30____34_______314 hoặc năm có 2 chữ số. Ghi chú. Chỉ có 4 chữ số có thể được phân tích cú pháp trên chế độ
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
32
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
33
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
342 chữ số năm
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
35
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
36Năm với bất kỳ số lượng chữ số và ký hiệu nào_______34_______37
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
38Quý của năm. Đặt tháng thành tháng đầu tiên trong quý.
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
39
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
40Month number
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
41
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
42Month name in locale set by
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
43
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
44
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
45Day of month
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
46
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
47Day of month with ordinal
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
48
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
49Day of year
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
50
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
51Unix timestamp
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
52
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
53Unix ms timestamp

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
30 từ phiên bản 2. 10. 5 hỗ trợ năm có 2 chữ số ở chế độ không phải
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
32 và chuyển đổi chúng thành năm gần 2000 (giống như
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
33)

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
35 đã được thêm vào 2. 11. 1. Nó sẽ khớp với bất kỳ số nào, đã ký hoặc chưa ký. Nó hữu ích cho những năm không có 4 chữ số hoặc trước kỷ nguyên chung. Nó có thể được sử dụng cho bất kỳ năm nào

Mã thông báo tuần năm, tuần và ngày trong tuần

Đối với những điều này, mã thông báo chữ thường sử dụng ngày bắt đầu tuần nhận biết ngôn ngữ và mã thông báo chữ hoa sử dụng ngày bắt đầu ngày của tuần ISO

Mã thông báo phân biệt chữ hoa chữ thường

InputExampleDescription
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
58
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
31Locale 4 digit week year
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
60
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
34Locale 2 digit week year
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
62
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
63Locale week of year
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
64
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
65Locale day of week
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
66
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
67Day name in locale set by
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
43
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
69
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
31ISO 4 digit week year
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
71
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
34ISO 2 digit week year
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
73
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
63ISO week of year
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
75
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
76ISO day of week

Các định dạng nhận biết ngôn ngữ

Các định dạng ngày và giờ nhận biết ngôn ngữ cũng có sẵn bằng cách sử dụng

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
77. Chúng đã được thêm vào trong phiên bản 2. 2. 1, ngoại trừ
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
78 đã được thêm vào 2. 8. 4

Mã thông báo phân biệt chữ hoa chữ thường

InputExampleDescription
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
79
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
80Date (in local format)
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
81
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
82Month name, day of month, year
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
83
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
84Month name, day of month, year, time
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
85
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
86Day of week, month name, day of month, year, time
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
87
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
88Time (without seconds)
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
78
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
90Time (with seconds)

Mã thông báo giờ, phút, giây, mili giây và độ lệch

Mã thông báo phân biệt chữ hoa chữ thường

Đầu vàoVí dụMô tả_______34_______91
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
92Giờ (thời gian 24 giờ)
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
93
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
40Giờ (thời gian 12 giờ được sử dụng với
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
95. )
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
96
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
97Hours (24 hour time from 1 to 24)
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
95
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
99Post or ante meridiem (Note the one character
moment(String);
00 are also considered valid)
moment(String);
01
moment(String);
02Minutes
moment(String);
03
moment(String);
02Seconds
moment(String);
05
moment(String);
06Fractional seconds
moment(String);
07
moment(String);
08Offset from UTC as
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
23,
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
24, or
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
26

Từ phiên bản 2. 10. 5. mã thông báo thứ hai phân số có độ dài từ 4 đến 9 có thể phân tích cú pháp bất kỳ số lượng chữ số nào, nhưng sẽ chỉ xem xét 3 chữ số hàng đầu (mili giây). Sử dụng nếu bạn có thời gian được in với nhiều chữ số phân số và muốn sử dụng đầu vào

Lưu ý rằng số lượng

moment(String);
12 ký tự được cung cấp chỉ liên quan khi phân tích cú pháp ở chế độ nghiêm ngặt. Ở chế độ tiêu chuẩn,
moment(String);
12,
moment(String);
14,
moment(String);
15,
moment(String);
16 đều tương đương và được hiểu là phân số của giây. Ví dụ:
moment(String);
17 luôn là 120 mili giây, việc chuyển qua
moment(String);
14 sẽ không khiến nó được hiểu là 12 mili giây

moment(String);
07 đã được thêm vào trong phiên bản 1. 2. 0

moment(String);
20 đã được thêm vào trong phiên bản 1. 6. 0

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
50 đã được thêm vào trong phiên bản 2. 0. 0

moment(String);
22 đã được thêm vào trong phiên bản 2. 10. 5

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
96 đã được thêm vào trong phiên bản 2. 13. 0

Trừ khi bạn chỉ định chênh lệch múi giờ, việc phân tích cú pháp một chuỗi sẽ tạo ra một ngày theo múi giờ hiện tại

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
6

Mã thông báo liên quan đến Năm kỷ nguyên

Mã thông báo phân biệt chữ hoa chữ thường

Đầu vàoVí dụMô tả. yyyy
moment(String);
24Yearsyo
moment(String);
25Ordinal YearsN
moment(String);
26Abbr Era nameNN
moment(String);
26Abbr Era nameNNN
moment(String);
26Abbr Era nameNNNN
moment(String);
29Full Era nameNNNNN
moment(String);
26Narrow Era name

Hỗ trợ kỷ nguyên đã được thêm vào trong 2. 25. 0. Mã thông báo/API vẫn đang thay đổi

Ghi chú và gotchas

Nếu thời điểm kết quả từ đầu vào được phân tích cú pháp không tồn tại, thì

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
28 sẽ trả về false

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
7

Kể từ phiên bản 2. 0. 0, khóa ngôn ngữ có thể được chuyển làm tham số thứ ba cho

moment(String);
32 và
moment(String);
33

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
8

Trình phân tích cú pháp của Moment rất dễ tha thứ và điều này có thể dẫn đến hành vi không mong muốn/không mong muốn

Ví dụ, hành vi sau đây có thể được quan sát

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
9

Trước 2. 13. 0 trình phân tích cú pháp thể hiện hành vi sau. Điều này đã được sửa chữa

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
0

Kể từ phiên bản 2. 3. 0, bạn có thể chỉ định một boolean cho đối số cuối cùng để khiến Moment sử dụng phân tích cú pháp nghiêm ngặt. Phân tích cú pháp nghiêm ngặt yêu cầu định dạng và đầu vào khớp chính xác, bao gồm cả dấu phân cách

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
1

Bạn có thể sử dụng cả ngôn ngữ và độ nghiêm ngặt

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
2

Phân tích cú pháp nghiêm ngặt thường là tùy chọn phân tích cú pháp tốt nhất. Để biết thêm thông tin về việc chọn phân tích cú pháp nghiêm ngặt và tha thứ, hãy xem phần

Phân tích cú pháp hai chữ số năm

Theo mặc định, năm có hai chữ số trên 68 được coi là vào những năm 1900 và những năm từ 68 trở xuống được coi là vào những năm 2000. Điều này có thể được thay đổi bằng cách thay thế phương thức

moment(String);
34. Đối số duy nhất của phương thức này là một chuỗi chứa hai năm do người dùng nhập vào và sẽ trả về năm dưới dạng số nguyên

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
3

Phân tích cú pháp giờ và phút được dán

Từ phiên bản 2. 11. 0 phân tích cú pháp

moment(String);
35,
moment(String);
36,
moment(String);
37 và
moment(String);
38 được hỗ trợ

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
4

1. 0. 0+

chỉnh sửa

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
5

Nếu bạn không biết định dạng chính xác của chuỗi đầu vào, nhưng biết nó có thể là một trong nhiều chuỗi, bạn có thể sử dụng một mảng định dạng

Điều này cũng giống như, chỉ khác là nó sẽ cố khớp đầu vào với nhiều định dạng

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
6

Bắt đầu từ phiên bản 2. 3. 0, Moment sử dụng một số phỏng đoán đơn giản để xác định định dạng nào sẽ sử dụng. theo thứ tự

  • Thích các định dạng dẫn đến ngày tháng hơn các định dạng không hợp lệ
  • Thích các định dạng phân tích nhiều chuỗi hơn ít hơn và sử dụng nhiều định dạng hơn ít hơn, tôi. e. thích phân tích cú pháp chặt chẽ hơn
  • Thích các định dạng sớm hơn trong mảng hơn sau này
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
7

Bạn cũng có thể chỉ định một đối số miền địa phương và nghiêm ngặt. Chúng hoạt động giống như trường hợp định dạng duy nhất

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
8

Ghi chú. Phân tích cú pháp nhiều định dạng chậm hơn đáng kể so với phân tích cú pháp một định dạng. Nếu bạn có thể tránh nó, việc phân tích cú pháp một định dạng sẽ nhanh hơn nhiều

1. 0. 0+

chỉnh sửa

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
9

Bạn có thể tạo một khoảnh khắc với một dãy số phản ánh các tham số được truyền cho new Date()

moment(String);
39

moment(String);
0

Bất kỳ giá trị nào trong năm qua là tùy chọn và sẽ mặc định là số thấp nhất có thể

moment(String);
1

Xây dựng với một mảng sẽ tạo ra một ngày trong múi giờ hiện tại. Để tạo một ngày từ một mảng tại UTC, hãy sử dụng

moment(String);
40

moment(String);
2

Ghi chú. Bởi vì điều này phản ánh các tham số

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
74 gốc, tháng, giờ, phút, giây và mili giây đều được lập chỉ mục bằng 0. Năm và ngày trong tháng được lập chỉ mục 1

Đây thường là nguyên nhân gây ra sự thất vọng, đặc biệt là với các tháng, vì vậy hãy lưu ý

Nếu ngày đại diện bởi mảng không tồn tại,

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
28 sẽ trả về false

moment(String);
3

1. 5. 0+

chỉnh sửa

moment(String);
4

Theo mặc định, thời điểm phân tích cú pháp và hiển thị theo giờ địa phương

Nếu bạn muốn phân tích cú pháp hoặc hiển thị một thời điểm theo giờ UTC, bạn có thể sử dụng

moment(String);
33 thay vì
moment(String);
32

Điều này đưa chúng ta đến một tính năng thú vị của Moment. js. chế độ UTC

Khi ở chế độ UTC, tất cả các phương thức hiển thị sẽ hiển thị theo giờ UTC thay vì giờ địa phương

moment(String);
5

Ngoài ra, khi ở chế độ UTC, tất cả các getter và setter sẽ sử dụng nội bộ các phương thức

moment(String);
45 và
moment(String);
46 thay vì các phương thức
moment(String);
47 và
moment(String);
48

moment(String);
6

Điều quan trọng cần lưu ý là mặc dù các màn hình khác nhau ở trên, nhưng cả hai đều có cùng thời điểm

moment(String);
7

Bất kỳ thời điểm nào được tạo bằng

moment(String);
33 sẽ ở chế độ UTC và bất kỳ thời điểm nào được tạo bằng
moment(String);
32 sẽ không

Để chuyển từ UTC sang giờ địa phương, bạn có thể sử dụng hoặc

moment(String);
8

2. 3. 0+

chỉnh sửa

moment(String);
9

Các chức năng phân tích chuỗi của Moment như

moment(String);
51 và
moment(String);
52 chấp nhận thông tin offset nếu được cung cấp, nhưng chuyển đổi đối tượng Moment kết quả thành thời gian cục bộ hoặc UTC. Ngược lại,
moment(String);
53 phân tích cú pháp chuỗi nhưng giữ đối tượng Khoảnh khắc kết quả trong múi giờ có độ lệch cố định với độ lệch được cung cấp trong chuỗi

var day = moment("1995-12-25");
0

Nó cũng cho phép bạn chuyển các đối số miền địa phương và mức độ nghiêm ngặt

var day = moment("1995-12-25");
1

moment(String);
54 tương đương với việc phân tích cú pháp chuỗi và sử dụng
moment(String);
55 để phân tích vùng

var day = moment("1995-12-25");
2

1. 7. 0+

chỉnh sửa

var day = moment("1995-12-25");
3

Moment áp dụng các quy tắc khởi tạo chặt chẽ hơn hàm tạo

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
74

var day = moment("1995-12-25");
4

Bạn có thể kiểm tra xem Khoảnh khắc có coi ngày không hợp lệ hay không bằng cách sử dụng

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
28. Bạn có thể kiểm tra các số liệu được sử dụng bởi
moment(String);
58 bằng cách sử dụng
moment(String);
59, trả về một đối tượng

Các cờ phân tích cú pháp sau dẫn đến một ngày không hợp lệ

  • moment(String);
    
    60. Trường ngày bị tràn, chẳng hạn như tháng thứ 13, ngày thứ 32 của tháng (hoặc ngày 29 tháng 2 đối với các năm không nhuận), ngày thứ 367 của năm, v.v.
    moment(String);
    
    60 chứa chỉ mục của đơn vị không hợp lệ để khớp với
    moment(String);
    
    62 (xem bên dưới);
  • moment(String);
    
    64. Tên tháng không hợp lệ, chẳng hạn như
    moment(String);
    
    65. Chứa chính chuỗi tháng không hợp lệ, nếu không thì null
  • moment(String);
    
    66. Một chuỗi đầu vào không chứa gì có thể phân tích cú pháp, chẳng hạn như
    moment(String);
    
    67. Boolean
  • moment(String);
    
    68. Một đầu vào
    moment(String);
    
    69, như
    moment(String);
    
    70. Boolean
  • moment(String);
    
    71. Một danh sách định dạng trống, chẳng hạn như
    moment(String);
    
    72. Boolean
  • moment(String);
    
    73. Ngày được tạo rõ ràng là không hợp lệ, chẳng hạn như
    moment(String);
    
    74. Boolean

Ngoài những điều trên, kể từ ngày 2. 13. 0, các cờ meridiem và parsedDateParts hoạt động cùng nhau để xác định tính hợp lệ của ngày

  • moment(String);
    
    75. Cho biết kinh tuyến (AM/PM) nào đã được phân tích cú pháp, nếu có. Chuỗi
  • moment(String);
    
    76. Trả về một mảng các phần ngày được phân tích cú pháp theo thứ tự giảm dần - i. e. parsedDateParts[0] === năm. Nếu không có bộ phận nào, nhưng kinh tuyến có giá trị, thì ngày không hợp lệ. Mảng

Ngoài ra, nếu Khoảnh khắc được phân tích cú pháp ở chế độ nghiêm ngặt, các cờ này phải trống để Khoảnh khắc hợp lệ

  • moment(String);
    
    77. mảng các chuỗi con định dạng không tìm thấy trong chuỗi đầu vào
  • moment(String);
    
    78. mảng các chuỗi con đầu vào không khớp với chuỗi định dạng

Ghi chú. Khái niệm về giá trị của Moment trở nên chặt chẽ và nhất quán hơn giữa 2. 2 và 2. 3. Ghi chú. Hiệu lực được xác định khi tạo thời điểm. Một khoảnh khắc sửa đổi (tôi. e.

moment(String);
79) sẽ vẫn có hiệu lực

Ngoài ra, bạn có thể sử dụng

moment(String);
80 để xác định đơn vị ngày nào bị tràn

var day = moment("1995-12-25");
5

Giá trị trả về có ý nghĩa như sau

  1. năm
  2. tháng
  3. ngày
  4. giờ
  5. phút
  6. giây
  7. mili giây

Ghi chú. Trong trường hợp có nhiều đơn vị sai, đơn vị đầu tiên được trả lại (ví dụ: vì hiệu lực của ngày có thể phụ thuộc vào tháng)

Khoảnh khắc không hợp lệ

Nếu một khoảnh khắc không hợp lệ, nó sẽ hoạt động giống như một NaN trong các phép toán dấu phẩy động

Tất cả những điều sau đây tạo ra những khoảnh khắc không hợp lệ

  • moment(String);
    
    81
  • moment(String);
    
    82
  • moment(String);
    
    83
  • moment(String);
    
    84
  • moment(String);
    
    85
  • moment(String);
    
    86
  • moment(String);
    
    87
  • moment(String);
    
    88
  • moment(String);
    
    89
  • moment(String);
    
    90
  • moment(String);
    
    91
  • moment(String);
    
    92

Sau đây tạo ra một phiên bản bản địa hóa của

moment(String);
93

  • moment(String);
    
    94 kết quả là
    moment(String);
    
    95 ở địa phương hiện tại
  • moment(String);
    
    96
  • moment(String);
    
    97
  • moment(String);
    
    98
  • moment(String);
    
    99
  • var day = moment("1995-12-25");
    
    00
  • var day = moment("1995-12-25");
    
    01
  • var day = moment("1995-12-25");
    
    02 (Trước 2. 18. 0)
  • var day = moment("1995-12-25");
    
    03

Sự trở lại sau

var day = moment("1995-12-25");
04

  • var day = moment("1995-12-25");
    
    05
  • var day = moment("1995-12-25");
    
    06
  • var day = moment("1995-12-25");
    
    07
  • var day = moment("1995-12-25");
    
    08
  • var day = moment("1995-12-25");
    
    09
  • var day = moment("1995-12-25");
    
    10
  • var day = moment("1995-12-25");
    
    11
  • var day = moment("1995-12-25");
    
    12
  • var day = moment("1995-12-25");
    
    13
  • var day = moment("1995-12-25");
    
    14
  • var day = moment("1995-12-25");
    
    15
  • var day = moment("1995-12-25");
    
    16
  • var day = moment("1995-12-25");
    
    17
  • var day = moment("1995-12-25");
    
    18
  • var day = moment("1995-12-25");
    
    19
  • var day = moment("1995-12-25");
    
    20
  • var day = moment("1995-12-25");
    
    21

Và những trả về

moment(String);
69 hoặc
var day = moment("1995-12-25");
23 với một số cấu trúc

  • var day = moment("1995-12-25");
    
    24 trả về giá trị rỗng, giống như tất cả các getter có tên khác
  • var day = moment("1995-12-25");
    
    25
  • var day = moment("1995-12-25");
    
    26 có tất cả các giá trị được đặt thành
    var day = moment("1995-12-25");
    
    23
  • var day = moment("1995-12-25");
    
    28 trả về một đối tượng Ngày không hợp lệ
  • var day = moment("1995-12-25");
    
    29 trả về giá trị rỗng
  • var day = moment("1995-12-25");
    
    30 trả về giá trị rỗng
  • var day = moment("1995-12-25");
    
    31 trả về giá trị rỗng
  • var day = moment("1995-12-25");
    
    02 trả về giá trị rỗng (Kể từ ngày 2. 18. 0)

1. 0. 0+

chỉnh sửa

var day = moment("1995-12-25");
6

Thay đổi khoảnh khắc ban đầu bằng cách thêm thời gian

Đây là một chức năng khá mạnh mẽ để thêm thời gian vào một thời điểm hiện tại. Để thêm thời gian, hãy nhập khóa thời gian bạn muốn thêm và số lượng bạn muốn thêm

var day = moment("1995-12-25");
7

Cũng có một số phím tốc ký nếu bạn thích toàn bộ thứ ngắn gọn đó

var day = moment("1995-12-25");
8KeyShorthandyearsyquartersQmonthsMweekswdaysdhourshminutesmsecondssmillisecondsms

Nếu bạn muốn thêm nhiều khóa khác nhau cùng một lúc, bạn có thể chuyển chúng vào dưới dạng đối tượng bằng chữ

var day = moment("1995-12-25");
9

There are no upper limits for the amounts, so you can overload any of the parameters

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
0

Special considerations for months and years

If the day of the month on the original date is greater than the number of days in the final month, the day of the month will change to the last day in the final month

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
1

There are also special considerations to keep in mind when adding time that crosses over daylight saving time. If you are adding years, months, weeks, or days, the original hour will always match the added hour

Adding a month will add the specified number of months to the date

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
2
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
3

If you are adding hours, minutes, seconds, or milliseconds, the assumption is that you want precision to the hour, and will result in a different hour

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
4

Alternatively, you can use to add to moments

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
5

Before version 2. 8. 0, the

var day = moment("1995-12-25");
33 syntax was also supported. It has been deprecated in favor of
var day = moment("1995-12-25");
34

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
6

As of 2. 12. 0 when decimal values are passed for days and months, they are rounded to the nearest integer. Weeks, quarters, and years are converted to days or months, and then rounded to the nearest integer

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
7

1. 7. 0+

chỉnh sửa

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
8

Mutates the original moment by setting it to the start of a unit of time

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
9

These shortcuts are essentially the same as the following

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
0
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
1

As of version 2. 0. 0,

var day = moment("1995-12-25");
35 replaced
var day = moment("1995-12-25");
36

Note.

var day = moment("1995-12-25");
37 was added in version 2. 0. 0

As of version 2. 1. 0,

var day = moment("1995-12-25");
37 uses the locale aware week start day

Note.

var day = moment("1995-12-25");
39 was added in version 2. 2. 0

Note.

var day = moment("1995-12-25");
40 was added as an alias for day in 2. 13. 0

2. 9. 0++

chỉnh sửa

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
2

Get or set the UTC offset in minutes

Note. Unlike this function returns the real offset from UTC, not the reverse offset (as returned by

var day = moment("1995-12-25");
42)

Getting the

var day = moment("1995-12-25");
43 of the current object

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
3

Setting the UTC offset by supplying minutes. The offset is set on the moment object that

var day = moment("1995-12-25");
44 is called on. If you are wanting to set the offset globally, try using moment-timezone. Note that once you set an offset, it's fixed and won't change on its own (i. e không có quy tắc DST). If you want an actual time zone -- time in a particular location, like
var day = moment("1995-12-25");
45, consider moment-timezone

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
4

If the input is less than

var day = moment("1995-12-25");
46 and greater than
var day = moment("1995-12-25");
47, it will interpret your input as hours instead

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
5

It is also possible to set the UTC offset from a string

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
6

moment(String);
55 will search the string for the last match of
var day = moment("1995-12-25");
49, so you can even pass an ISO8601 formatted string with offset and the moment will be changed to that UTC offset

Note that if the string does not include 'Z', it must include the

var day = moment("1995-12-25");
50 or
var day = moment("1995-12-25");
51 character

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
7

The

var day = moment("1995-12-25");
43 function has an optional second parameter which accepts a boolean value indicating whether to keep the existing time of day

  • Passing

    var day = moment("1995-12-25");
    
    04 (the default) will keep the same instant in Universal Time, but the local time will change

  • Passing

    var day = moment("1995-12-25");
    
    54 will keep the same local time, but at the expense of choosing a different point in Universal Time

One use of this feature is if you want to construct a moment with a specific time zone offset using only numeric input values

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
8

1. 0. 0+

chỉnh sửa

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
9

This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
0TokenOutputMonthM1 2 . 11 12Mo1st 2nd . 11th 12thMM01 02 . 11 12MMMJan Feb . Nov DecMMMMJanuary February . November DecemberQuarterQ1 2 3 4Qo1st 2nd 3rd 4thDay of MonthD1 2 . 30 31Do1st 2nd . 30th 31stDD01 02 . 30 31Day of YearDDD1 2 . 364 365DDDo1st 2nd . 364th 365thDDDD001 002 . 364 365Day of Weekd0 1 . 5 6do0th 1st . 5th 6thddSu Mo . Fr SadddSun Mon . Fri SatddddSunday Monday . Friday SaturdayDay of Week (Locale)e0 1 . 5 6Day of Week (ISO)E1 2 . 6 7Week of Yearw1 2 . 52 53wo1st 2nd . 52nd 53rdww01 02 . 52 53Week of Year (ISO)W1 2 . 52 53Wo1st 2nd . 52nd 53rdWW01 02 . 52 53YearYY70 71 . 29 30YYYY1970 1971 . 2029 2030YYYYYY-001970 -001971 . +001907 +001971
Note. (Bao trùm phạm vi giá trị toàn thời gian xấp xỉ 273.790 năm tiến hoặc lùi kể từ ngày 01 tháng 01 năm 1970)Y1970 1971. 9999 +10000 +10001
Ghi chú. Điều này tuân thủ tiêu chuẩn ISO 8601 cho các ngày sau năm 9999Era Yeary1 2. 2020. Thời đạiN, NN, NNNBC AD
Ghi chú. Tên thời đại AbbrNNNN Before Christ, Anno Domini
Ghi chú. Tên thời đại đầy đủNNNNNBC AD
Ghi chú. Tên thời đại thu hẹpTuần Nămgg70 71. 29 30gggg1970 1971. 2029 2030Tuần Năm (ISO)GG70 71. 29 30GGGG1970 1971 . 2029 2030AM/PMAAM PMMaam pmHourH0 1. 22 23HH00 01 . 22 23h1 2 . 11 12hh01 02 . 11 12k1 2 . 23 24kk01 02 . 23 24Minutem0 1 . 58 59mm00 01 . 58 59Seconds0 1 . 58 59ss00 01 . 58 59Fractional SecondS0 1 . 8 9SS00 01 . 98 99SSS000 001 . 998 999SSSS . SSSSSSSSS000[0. ] 001[0. ] . 998[0. ] 999[0. ]Time Zonez or zzEST CST . MST PST
Note. as of 1. 6. 0, the z/zz format tokens have been deprecated from plain moment objects. Read more about it here. However, they *do* work if you are using a specific time zone with the moment-timezone addon. Z-07. 00 -06. 00 . +06. 00 +07. 00ZZ-0700 -0600 . +0600 +0700Unix TimestampX1360013296Unix Millisecond Timestampx1360013296123

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
50 was added in 2. 0. 0

var day = moment("1995-12-25");
56 were added in 2. 1. 0

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
52 was added in 2. 8. 4

moment(String);
16 to
var day = moment("1995-12-25");
59 were added in 2. 10. 5. They display 3 significant digits and the rest is filled with zeros

var day = moment("1995-12-25");
60 and
var day = moment("1995-12-25");
61 were added in 2. 13. 0

Localized formats

Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale

There are upper and lower case variations on the same formats. The lowercase version is intended to be the shortened version of its uppercase counterpart

TimeLT8. 30 PMTime with secondsLTS8. 30. 25 PMMonth numeral, day of month, yearL09/04/1986l9/4/1986Month name, day of month, yearLLSeptember 4, 1986llSep 4, 1986Month name, day of month, year, timeLLLSeptember 4, 1986 8. 30 PMlllSep 4, 1986 8. 30 PMMonth name, day of month, day of week, year, timeLLLLThursday, September 4, 1986 8. 30 PMllllThu, ngày 4 tháng 9 năm 1986 8. 30 PM

var day = moment("1995-12-25");
62 are available in 2. 0. 0.
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
78 was added in 2. 8. 4

Escaping characters

To escape characters in format strings, you can wrap the characters in square brackets

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
1

Similarities and differences with LDML

Note. While these date formats are very similar to LDML date formats, there are a few minor differences regarding day of month, day of year, and day of week

For a breakdown of a few different date formatting tokens across different locales, see

Formatting speed

To compare Moment. js formatting speed against other libraries, check out this comparison against other libraries

Other tokens

If you are more comfortable working with strftime instead of LDML-like parsing tokens, you can use Ben Oakes' plugin. benjaminoakes/moment-strftime

Default format

Calling

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
29 without a format will default to
var day = moment("1995-12-25");
65. Out of the box,
var day = moment("1995-12-25");
65 is the ISO8601 format
var day = moment("1995-12-25");
67

As of version 2. 13. 0, when in UTC mode, the default format is governed by

var day = moment("1995-12-25");
68 which is in the format
var day = moment("1995-12-25");
69. This returns
require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
26 as the offset, instead of
var day = moment("1995-12-25");
71

In certain instances, a local timezone (such as

var day = moment("1995-12-25");
72) may have a zero offset, and will be considered to be UTC. In such cases, it may be useful to set
var day = moment("1995-12-25");
65 and
var day = moment("1995-12-25");
68 to use the same formatting

Changing the value of

var day = moment("1995-12-25");
65 will only affect formatting, and will not affect parsing. for example

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
2

1. 0. 0+

chỉnh sửa

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
3

A common way of displaying time is handled by

var day = moment("1995-12-25");
76. This is sometimes called timeago or relative time

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
4

If you pass

var day = moment("1995-12-25");
54, you can get the value without the suffix

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
5

The base strings are . Time is rounded to the nearest second

The breakdown of which string is displayed for each length of time is outlined in the table below

RangeKeySample Output0 to 44 secondssa few seconds agounsetss44 seconds ago45 to 89 secondsma minute ago90 seconds to 44 minutesmm2 minutes ago . 44 minutes ago45 to 89 minuteshan hour ago90 minutes to 21 hourshh2 hours ago . 21 hours ago22 to 35 hoursda day ago36 hours to 25 daysdd2 days ago . 25 days ago26 to 45 daysMa month ago45 to 319 daysMM2 months ago . 10 months ago320 to 547 days (1. 5 years)ya year ago548 days+yy2 years ago . 20 years ago

Note. From version 2. 10. 3, if the target moment object is invalid the result is the localized Invalid date string

Note. The

var day = moment("1995-12-25");
78 key was added in 2. 18. 0. It is an optional threshold. It will never display UNLESS the user manually sets the ss threshold. Until the
var day = moment("1995-12-25");
78 threshold is set, it defaults to the value of the
var day = moment("1995-12-25");
80 threshold minus 1 (so, invisible to the user)

2. 10. 3+

chỉnh sửa

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
6

A common way of displaying time is handled by

var day = moment("1995-12-25");
81. This is sometimes called timeago or relative time

This is similar to , but gives the opposite interval.

var day = moment("1995-12-25");
83

This is similar to , but is special-cased for the current time. Use

var day = moment("1995-12-25");
84, if you want to control the two end points of the interval

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
7

If you pass

var day = moment("1995-12-25");
54, you can get the value without the prefix

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
8

Các chuỗi cơ sở là

The breakdown of which string is displayed for each length of time is outlined in the table below

RangeKeySample Output0 đến 44 giây trong giây45 đến 89 giâyphút một phút90 giây đến 44 phútmmin 2 phút. trong 44 phút45 đến 89 phút hin một giờ90 phút đến 21 giờ hin 2 giờ. trong 21 giờ22 đến 35 giờ trong một ngày36 giờ đến 25 ngàyddin 2 ngày. in 25 days26 to 45 daysMin a month45 to 319 daysMMin 2 months . trong 10 tháng320 đến 547 ngày (1. 5 năm) âm một năm548 ngày+yyin 2 năm. Trong 20 năm

Từ phiên bản 2. 10. 3, nếu đối tượng thời điểm đích không hợp lệ, kết quả là chuỗi ngày không hợp lệ được bản địa hóa

1. 3. 0+

chỉnh sửa

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part
2013-039 09    # An ordinal date part and hour time part
9

Thời gian theo lịch hiển thị thời gian liên quan đến một

var day = moment("1995-12-25");
87 nhất định (mặc định là bắt đầu từ ngày hôm nay), nhưng hiển thị hơi khác so với thời gian của
var day = moment("1995-12-25");
76

var day = moment("1995-12-25");
89 sẽ định dạng một ngày với các chuỗi khác nhau tùy thuộc vào mức độ gần với ngày của
var day = moment("1995-12-25");
87 (hôm nay theo mặc định) là ngày

Tuần trước Thứ Hai tuần trước lúc 2. 30 Ngày hôm trước Hôm qua lúc 2. 30 Cùng ngày Hôm nay lúc 2. 30 AMNgày hôm sauNgày mai lúc 2. 30 AMTuần tớiChủ nhật lúc 2. 30 AMMọi thứ khác7/10/2011

Các chuỗi này được bản địa hóa và

từ 2. 10. 5 thời điểm hỗ trợ chỉ định định dạng đầu ra lịch cho mỗi lần gọi

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
0

var day = moment("1995-12-25");
91 được sử dụng làm định dạng khi thời điểm cách ngày
var day = moment("1995-12-25");
87 hơn một tuần

Ghi chú. Từ phiên bản 2. 14. 0, đối số định dạng cho lịch có thể là lệnh gọi lại được thực thi trong ngữ cảnh thời điểm với một đối số duy nhất ngay bây giờ

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
1

Note. From version 2. 25. 0 you can only pass a formats argument, it could be an object of strings and functions

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
2

1. 0. 0+

chỉnh sửa

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
3

To get the difference in milliseconds, use

var day = moment("1995-12-25");
93 like you would use
var day = moment("1995-12-25");
94

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
4

To get the difference in another unit of measurement, pass that measurement as the second argument

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
5

To get the duration of a difference between two moments, you can pass

var day = moment("1995-12-25");
95 as an argument into
var day = moment("1995-12-25");
96. See the docs on for more info

The supported measurements are

var day = moment("1995-12-25");
97,
var day = moment("1995-12-25");
98,
var day = moment("1995-12-25");
99,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
00,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
01,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
02, and
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
03. For ease of development, the singular forms are supported as of 2. 0. 0. Units of measurement other than milliseconds are available in version 1. 1. 1

Theo mặc định,

var day = moment("1995-12-25");
93 sẽ cắt bớt kết quả đến 0 chữ số thập phân, trả về một số nguyên. If you want a floating point number, pass
var day = moment("1995-12-25");
54 as the third argument. Before 2. 0. 0,
var day = moment("1995-12-25");
93 returned a number rounded to the nearest integer, not a truncated number

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
6

If the moment is earlier than the moment you are passing to

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
07, the return value will be negative

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
7

An easy way to think of this is by replacing

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
08 with a minus operator

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
8

Month and year diffs

var day = moment("1995-12-25");
93 has some special handling for month and year diffs. It is optimized to ensure that two months with the same date are always a whole number apart

So Jan 15 to Feb 15 should be exactly 1 month

Feb 28 to Mar 28 should be exactly 1 month

Feb 28 2011 to Feb 28 2012 should be exactly 1 year

See more discussion on the month and year diffs here

This change to month and year diffs was made in 2. 0. 0. As of version 2. 9. 0 diff also support quarter unit

2. 0. 0+

chỉnh sửa

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm
2013-02-08 09:30:26.123+07     # +-HH
9

Check if a moment is before another moment. The first argument will be parsed as a moment, if not already so

moment("not a real date").isValid(); // false
0

If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter

As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day

moment("not a real date").isValid(); // false
1

Like

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
10 and
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
11, any of the units of time that are supported for
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
12 are supported for
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
13

moment("not a real date").isValid(); // false
2

If nothing is passed to

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
13, it will default to the current time

NOTE.

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
15 has undefined behavior and should not be used. If the code runs fast the initial created moment would be the same as the one created in isBefore to perform the check, so the result would be
var day = moment("1995-12-25");
04. But if the code runs slower it's possible that the moment created in isBefore is measurably after the one created in
moment(String);
32, so the call would return
var day = moment("1995-12-25");
54

2. 0. 0+

chỉnh sửa

moment("not a real date").isValid(); // false
3

Check if a moment is the same as another moment. The first argument will be parsed as a moment, if not already so

moment("not a real date").isValid(); // false
4

If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter

moment("not a real date").isValid(); // false
5

When including a second parameter, it will match all units equal or larger. Passing in

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
19 will check
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
19 and
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
21. Passing in
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
22 will check
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
22,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
19, and
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
21

moment("not a real date").isValid(); // false
6

Like

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
10 and
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
13, any of the units of time that are supported for
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
12 are supported for
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
11

moment("not a real date").isValid(); // false
2

If the two moments have different timezones, the timezone of the first moment will be used for the comparison

moment("not a real date").isValid(); // false
8

NOTE.

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
30 has undefined behavior and should not be used. If the code runs fast the initial created moment would be the same as the one created in isSame to perform the check, so the result would be
var day = moment("1995-12-25");
54. But if the code runs slower it's possible that the moment created in isSame is measurably after the one created in
moment(String);
32, so the call would return
var day = moment("1995-12-25");
04

2. 9. 0+

chỉnh sửa

moment("not a real date").isValid(); // false
9

Check if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc). The match is exclusive. The first two arguments will be parsed as moments, if not already so

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
00

Note that the order of the two arguments matter. ngày "nhỏ hơn" phải ở đối số đầu tiên

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
01

Nếu bạn muốn giới hạn mức độ chi tiết ở một đơn vị khác với mili giây, hãy chuyển các đơn vị này làm tham số thứ ba

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
02

Like

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
11,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
13,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
10 any of the units of time that are supported for
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
12 are supported for
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
38. Year, month, week, isoWeek, day, hour, minute, and second

Version 2. 13. 0 introduces inclusivity. Một

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
39 biểu thị bao gồm một giá trị. A
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
40 biểu thị loại trừ. If the inclusivity parameter is used, both indicators must be passed

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
03

Note that in the event that the

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
41 and
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
42 parameters are the same, but the inclusivity parameters are different, false will preside

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
04

Nếu tham số bao gồm không được chỉ định, Moment sẽ mặc định là

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
43

1. 2. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
05

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
44 kiểm tra xem thời điểm hiện tại có phải là giờ mùa hè không

GHI CHÚ. Chức năng này là một HACK. thời điểm không có cách nào để biết liệu một thời điểm nhất định có ở DST thực tế hay không. Một số thay đổi về thời gian trong một vùng có liên quan đến DST, một số thì không và không có thông tin múi giờ hoàn chỉnh thì không thể biết được

Khoảnh khắc hiện đang kiểm tra thời gian mùa đông và mùa hè, và nếu phần bù khớp với phần bù mùa hè (và thời gian nghỉ hè khác với thời gian nghỉ mùa đông), thì nó sẽ báo cáo DST. Điều này hoạt động trong phần lớn các trường hợp, nhưng như đã đề cập ở trên, không "chính xác" và sẽ không hoạt động trong mọi trường hợp. Vì vậy, đừng đến với chúng tôi phàn nàn

Múi giờ thời điểm sự kiện (tại thời điểm viết 0. 5. 37) không hỗ trợ thông tin DST (i. e có phải là đồng hồ chính thức trong DST tại một thời điểm nhất định hay không), vì vậy để mọi thứ trở nên tốt hơn, một số nội dung mới (và gói tzdata) phải diễn ra theo múi giờ

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
06

1. 0. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
07

Theo mặc định, Khoảnh khắc. js đi kèm với chuỗi ngôn ngữ tiếng Anh (Hoa Kỳ). Nếu bạn cần các ngôn ngữ khác, bạn có thể tải chúng vào Khoảnh khắc. js để sử dụng sau

Để tải một ngôn ngữ, hãy chuyển khóa và các giá trị chuỗi tới

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
45

Bạn có thể tìm thêm chi tiết về từng phần của gói ngôn ngữ trong phần

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
08

Thông tin chi tiết về

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
46 và
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
47 có thể được tìm thấy trong phần

Sau khi bạn tải một ngôn ngữ, nó sẽ trở thành ngôn ngữ hoạt động. Để thay đổi ngôn ngữ đang hoạt động, chỉ cần gọi

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
45 bằng khóa của ngôn ngữ đã tải

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
09

kể từ 2. 21. 0, Khoảnh khắc sẽ

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
49 nếu ngôn ngữ không khả dụng

kể từ 2. 8. 0, việc thay đổi ngôn ngữ chung không ảnh hưởng đến các phiên bản hiện có

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
10

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
45 trả về ngôn ngữ được sử dụng. Điều này hữu ích vì Khoảnh khắc sẽ không thay đổi ngôn ngữ nếu nó không biết ngôn ngữ bạn chỉ định

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
11

Bạn cũng có thể chỉ định một danh sách các ngôn ngữ và Moment sẽ sử dụng danh sách đầu tiên mà nó có bản địa hóa cho

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
12

Moment cũng sẽ thử các chuỗi con của trình xác định ngôn ngữ từ cụ thể nhất đến ít cụ thể nhất cho đến khi tìm thấy một ngôn ngữ mà nó biết. Điều này hữu ích khi cung cấp Khoảnh khắc bằng chuỗi ngôn ngữ được lấy từ môi trường của người dùng, chẳng hạn như

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
51

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
13

Cuối cùng, Moment sẽ tìm kiếm một cách thông minh thông qua một loạt các ngôn ngữ và chuỗi con của chúng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
14

Logic hoạt động như sau -- ngôn ngữ tiếp theo được chọn và thử nguyên trạng. Nếu không thành công, mã thường cố gắng cắt bit cuối cùng (thường là ký hiệu quốc gia) và thử lại. Tuy nhiên, nếu phần tử mảng tiếp theo có tiền tố giống hoặc dài hơn phần tử được thử, thì quá trình lặp lại tiếp tục. Vì vậy, ví dụ nếu mảng có trình tự

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
15

sau đó thử "AA-BB" đầu tiên, sau đó một giải pháp ngây thơ sẽ thử "AA", nhưng thay vào đó, giải pháp này sẽ kiểm tra xem "AA-CC" thực sự cụ thể hơn "AA", vì vậy, nó thử "AA-CC" . Vì vậy, cuối cùng, các ngôn ngữ sau được thử theo thứ tự này (giả sử tất cả đều không thành công nên ngôn ngữ tiếp theo sẽ được thử)

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
16

1. 7. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
17

Cấu hình ngôn ngữ chung có thể gặp sự cố khi chuyển qua các khoảnh khắc có thể cần được định dạng thành ngôn ngữ khác

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
18

Nếu bạn gọi

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
52 mà không có tham số, bạn sẽ lấy lại cấu hình ngôn ngữ sẽ được sử dụng cho thời điểm đó

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
19

Nếu bạn cần truy cập dữ liệu ngôn ngữ trong giây lát, đây là cách ưu tiên để làm như vậy

kể từ 2. 3. 0, bạn cũng có thể chỉ định một mảng các mã định danh ngôn ngữ. Nó hoạt động giống như cách nó làm trong

2. 3. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
20

Đôi khi, rất hữu ích khi lấy danh sách các tháng hoặc ngày trong tuần ở một ngôn ngữ, chẳng hạn như khi điền vào menu thả xuống

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
21

Trả về danh sách các tháng ở ngôn ngữ hiện tại

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
22

Tương tự,

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
53 trả về tên tháng viết tắt và
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
54,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
55,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
56 trả về danh sách các ngày trong tuần

Bạn có thể chuyển một số nguyên vào từng hàm đó để biết tháng hoặc ngày trong tuần cụ thể

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
23

kể từ 2. 13. 0, bạn có thể chuyển một bool làm tham số đầu tiên của các hàm ngày trong tuần. Nếu đúng, các ngày trong tuần sẽ được trả về theo thứ tự ngôn ngữ cụ thể. Chẳng hạn, trong tiếng Ả Rập, Thứ Bảy là ngày đầu tiên trong tuần, do đó

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
24

Ghi chú. Không có tham số cụ thể của ngôn ngữ, các ngày trong tuần luôn có Chủ nhật là chỉ số 0, bất kể ngày đầu tuần của địa phương đó là gì

Một số địa phương đưa ra những cân nhắc đặc biệt khi định dạng tên tháng. Ví dụ: định dạng tiếng Hà Lan viết tắt tháng không có dấu chấm, nhưng chỉ khi nó định dạng tháng giữa các dấu gạch ngang. Phương thức

var day = moment("1995-12-25");
98 hỗ trợ chuyển định dạng để các tháng sẽ được liệt kê trong ngữ cảnh phù hợp

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
25

Và cuối cùng, bạn có thể kết hợp cả tùy chọn định dạng và tùy chọn số nguyên

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
26

2. 8. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
27

Bạn có thể truy cập các thuộc tính của ngôn ngữ hiện được tải thông qua hàm

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
58. Nó trả về ngôn ngữ hiện tại hoặc một ngôn ngữ với khóa đã cho

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
28

Đối tượng được trả về có các phương thức sau

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
29

Thông tin chi tiết về

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
59 có thể được tìm thấy trong phần

Khoảng khăc. js rất dễ tùy chỉnh. Nói chung, bạn nên tạo cài đặt ngôn ngữ với các tùy chỉnh của mình

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
30

Bạn có thể xóa một ngôn ngữ đã xác định trước đó bằng cách chuyển

moment(String);
69 làm đối số thứ hai. Ngôn ngữ đã xóa sẽ không còn khả dụng để sử dụng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
31

kể từ 2. 12. 0, có thể tạo ngôn ngữ kế thừa từ ngôn ngữ gốc

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
32

Các thuộc tính không được chỉ định trong ngôn ngữ sẽ được kế thừa từ ngôn ngữ gốc

kể từ 2. 16. 0, có thể xác định ngôn ngữ có cha mẹ chưa được xác định hoặc tải

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
33

kể từ 2. 21. 0 khi cố gắng tạo một khoảnh khắc với ngôn ngữ mới được xác định, khoảnh khắc sẽ cố tải cha mẹ xuống nếu nó tồn tại. Không thành công, nó sẽ mặc định cha mẹ thành ngôn ngữ toàn cầu

As of 2. 12. 0 cũng có thể cập nhật các thuộc tính của ngôn ngữ

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
34

Bất kỳ thuộc tính nào được chỉ định sẽ được cập nhật, trong khi các thuộc tính khác sẽ giữ nguyên. Chức năng này không ảnh hưởng đến những khoảnh khắc đã tồn tại. Lưu ý rằng việc gọi

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
61 cũng thay đổi ngôn ngữ toàn cầu hiện tại thành ngôn ngữ được cập nhật;

Để hoàn nguyên một bản cập nhật, hãy sử dụng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
35

2. 12. 0 không dùng nữa bằng cách sử dụng

require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
43 để thay đổi ngôn ngữ hiện có. Sử dụng
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
63 để thay thế

1. 0. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
36

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
64 phải là một mảng tên tháng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
37

Nếu bạn cần xử lý nhiều hơn để tính tên của tháng, (ví dụ: nếu có ngữ pháp khác nhau cho các định dạng khác nhau), thì

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
64 có thể là một hàm có chữ ký sau. Nó phải luôn trả về một tên tháng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
38

Từ phiên bản 2. 11. 0 tháng cũng có thể là một đối tượng, chỉ định các hình thức

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
66 và
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
67 (chỉ định và buộc tội). Biểu thức chính quy được chạy trên định dạng để kiểm tra xem có sử dụng biểu mẫu
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
67 hay không là
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
69. Từ phiên bản 2. 14. 0 một số khác có thể được chỉ định bằng phím
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
70

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
39

1. 0. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
40

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
71 phải là một mảng tên các ngày trong tuần

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
41

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
71 cũng có thể là chức năng gọi lại

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
42

Ghi chú. Từ phiên bản 2. 11. 0 định dạng/trường hợp độc lập cũng có thể được thông qua.

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
70 sẽ được sử dụng đối với chuỗi định dạng đầy đủ để xác định nên sử dụng biểu mẫu nào

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
43

1. 0. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
44

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
74 phải là một đối tượng của chuỗi thay thế cho
var day = moment("1995-12-25");
94

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
45

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
76 đề cập đến tiền tố/hậu tố cho những ngày trong tương lai và
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
77 đề cập đến tiền tố/hậu tố cho những ngày trong quá khứ. Đối với tất cả những người khác, một ký tự đơn đề cập đến số ít và một ký tự kép đề cập đến số nhiều

Nếu một ngôn ngữ yêu cầu xử lý bổ sung cho mã thông báo, thì nó có thể đặt mã thông báo làm hàm có chữ ký sau. Hàm sẽ trả về một chuỗi

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
46

Đối số

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
78 đề cập đến khóa thay thế trong đối tượng
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
74. (ví dụ.
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
80, v.v. )

Đối số

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
81 đề cập đến số lượng đơn vị cho khóa đó. Đối với
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
82, số này là số phút, v.v.

Đối số

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
83 sẽ đúng nếu mã thông báo sẽ được hiển thị mà không có hậu tố và sai nếu nó sẽ được hiển thị với hậu tố. (Lý do logic đảo ngược là vì hành vi mặc định là hiển thị với hậu tố. )

Đối số

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
84 sẽ đúng nếu nó sẽ sử dụng hậu tố/tiền tố tương lai và sai nếu nó sẽ sử dụng tiền tố/hậu tố quá khứ

Ghi chú. Xử lý cho

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
85 và
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
86 đã được thêm vào 2. 25. 0

2. 7. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
47

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
87 có các ngưỡng xác định thời điểm một đơn vị được coi là phút, giờ, v.v. Ví dụ: theo mặc định, hơn 45 giây được coi là một phút, hơn 22 giờ được coi là một ngày, v.v. Để thay đổi các giới hạn đó, hãy sử dụng
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
88 trong đó đơn vị là một trong số
var day = moment("1995-12-25");
78,
var day = moment("1995-12-25");
80,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
82,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
92,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
93,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
85,
2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
95

đơn vị nghĩa là số lần sử dụng là vài giây số giây nhỏ nhất được tính bằng giây, trừ 1. Phải được đặt sau khi đặt đơn vị `s` hoặc không đặt đơn vị `s`. ssecondsố giây ít nhất được coi là một phút. mminutes số phút ít nhất được coi là một giờ. hhourssố giờ ít nhất được coi là một ngày. ddaysít nhất số ngày được coi là một tuần. wweeksố tuần ít nhất được coi là một tháng. Không được sử dụng theo mặc định. Mtháng số tháng ít nhất được coi là một năm.
// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
48

Ghi chú. Đơn vị tuần đã được thêm vào trong 2. 25. 0. Theo mặc định, nó không được sử dụng (đặt thành null), nhưng bạn có thể đặt thành giá trị khác null và cũng (tùy chọn) đặt

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
93 thấp hơn, để nó chuyển từ vài ngày sang vài tuần trước đó

Ghi chú. Truy xuất ngưỡng đã được thêm vào trong 2. 8. 1

Ghi chú. Truy xuất và đặt ngưỡng

var day = moment("1995-12-25");
78 đã được thêm vào 2. 18. 0

1. 0. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
49

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
98 phải là một số nguyên đại diện cho ngày đầu tuần, 0 là Chủ Nhật, 1 là Thứ Hai,. , 6 là thứ bảy

2013-02-08  # A calendar date part
2013-02     # A month date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

20130208    # Basic (short) full date
201303      # Basic (short) year+month
2013        # Basic (short) year only
2013W065    # Basic (short) week, weekday
2013W06     # Basic (short) week only
2013050     # Basic (short) ordinal date (year + day-of-year)
99 phải là số nguyên.
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
00 được sử dụng cùng với
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
01 để xác định tuần đầu tiên của năm.
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
00 được tính là
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
03, trong đó
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
04 là ngày đầu tiên của tháng 1 phải thuộc tuần đầu tiên của năm

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
50

2. 25. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
51

Chỉ định thời đại cho một ngôn ngữ cụ thể. Một thời đại là một khoảng thời gian với tên và đánh số năm. Số năm tuyệt đối (như 2020) cũng có thể được chỉ định là 2020 AD. năm thứ 2020 của kỷ nguyên sau công nguyên. Tương tự, số năm tuyệt đối -0500 có thể được mô tả là 501 trước Công nguyên, năm thứ 501 kể từ thời đại trước Công nguyên

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
52

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
05 và
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
06 chi phối hướng đi của thời đại. Như trong trường hợp của
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
07, nó phát triển về phía
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
08, do đó
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
05 >
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
06. Đối với các thời đại tăng dần về phía +Vô cực
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
05 <
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
06

Phân tích cú pháp/định dạng thời đại được thực hiện bằng mã thông báo

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
13,
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
14 và
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
15

Ghi chú. Các API liên quan đến thời đại có thể thay đổi

1. 6. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
53

Để tạo khoảng thời gian, hãy gọi

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
16 với khoảng thời gian tính bằng mili giây

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
54

Nếu bạn muốn tạo khoảnh khắc với đơn vị đo khác mili giây, bạn có thể truyền đơn vị đo cũng được

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
55

Cách viết tắt tương tự cho

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
17 và
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
18 cũng hoạt động ở đây

KeyShorthandyearsymonthsMweekswdaysdhourshminutesmsecondssmillisecondsms

Giống như

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
17, bạn có thể truyền một đối tượng giá trị nếu bạn cần nhiều đơn vị đo lường khác nhau

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
56

kể từ 2. 1. 0, thời điểm hỗ trợ phân tích cú pháp ASP. NET phong cách khoảng thời gian. Các định dạng sau được hỗ trợ

Định dạng là một chuỗi giờ, phút, giây được phân tách bằng dấu hai chấm như

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
20. Số ngày có thể được bắt đầu bằng dấu phân cách dấu chấm như vậy
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
21. Một phần giây cũng được hỗ trợ
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
22

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
57

kể từ 2. 3. 0, thời điểm cũng hỗ trợ phân tích thời lượng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
58

kể từ 2. 11. 0, chuỗi định dạng thời lượng có khoảng cách giữa ngày và phần còn lại được hỗ trợ

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
59

kể từ 2. 13. 0, các dấu hiệu âm và dương hỗn hợp được hỗ trợ khi phân tích thời lượng

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
60

kể từ 2. 18. 0, thời lượng không hợp lệ được hỗ trợ, tương tự như thời điểm không hợp lệ. Để tạo khoảng thời gian không hợp lệ, bạn có thể chuyển

var day = moment("1995-12-25");
23 cho giá trị của một đơn vị

Trong các bản phát hành sắp tới, thời lượng không hợp lệ sẽ bao gồm nhiều trường hợp hơn (như giá trị null cho đơn vị)

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
61

1. 6. 0+

chỉnh sửa

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
62

Đôi khi, bạn muốn tất cả những điều tốt đẹp của

var day = moment("1995-12-25");
94 nhưng bạn không muốn phải tạo hai khoảnh khắc, bạn chỉ muốn hiển thị một khoảng thời gian

Nhập

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
25

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
63

Theo mặc định, chuỗi trả về mô tả khoảng thời gian

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
26 (không có hậu tố). Nếu bạn muốn thời lượng định hướng
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
27,
2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
28 (có hậu tố), hãy chuyển true như bên dưới

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
64

Đối với các hậu tố trước đây, hãy chuyển vào một số âm

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
65

Thời lượng không hợp lệ được nhân hóa thành phiên bản địa phương hóa của

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

20130208T080910,123      # Short date and time up to ms, separated by comma
20130208T080910.123      # Short date and time up to ms
20130208T080910          # Short date and time up to seconds
20130208T0809            # Short date and time up to minutes
20130208T08              # Short date and time, hours only
29

// only needing core
define(['moment'], function (moment) {
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'
});

// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
    moment.locale('de');
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});

// async load locale
define(['require', 'moment'], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require(['moment/locale/de'], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format('LLLL'));  // 'Friday, June 24, 2016 1:42 AM'

    moment.locale('de');
    // Use moment now that the locale has been properly set.
    console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  })
});
66

Đầu ra nhân hóa có thể được cấu hình với các ngưỡng thời gian tương đối. Để chỉ định các ngưỡng cho một lời gọi nhân hóa cụ thể, hãy chuyển chúng dưới dạng đối số duy nhất hoặc sau hậu tố arg