且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用moment.js转换日期格式

更新时间:2023-01-31 14:40:25

$(function () {
  $('.date').each(function (index, dateElem) {
    var $dateElem = $(dateElem);
    var formatted = moment($dateElem.text(), 'MM-DD-YYYY').format('MMMM D');
    $dateElem.text(formatted);
   })
 });​

http://jsfiddle.net/x2fDP/

第2部分,尝试使用 new Date()。getTime() - textMoment.valueOf()(其中textMoment是从div文本创建的解析时刻实例)以获得毫秒数之前该日期是,如果该数字低于您的阈值,请使用 $ dateElem.addClass('new');

For part 2, try using new Date().getTime() - textMoment.valueOf()(where textMoment is the parsed moment instance created from the div's text) to obtain the number of milliseconds ago that date was and if that number is below your threshold, use $dateElem.addClass('new');