javascript日期处理库-Datejs.js

  1. 当天时间

    Date.today();
    
  2. 比较两个时间大小,返回-1,0,1

    var today = Date.today();
    var past = Date.today().add(-6).days();
    var future = Date.today().add(6).days();
    Date.compare(today, future);                    // -1
    Date.compare(today, new Date().clearTime());    // 0
    Date.compare(today, past)
    
  3. 比较时间是否在两个时间内(闭区间)

    var past = new Date(2000, 4, 5);
    var future = new Date(2010, 11, 25)
    Date.today().between(past, future); // true|false
    
  4. 是否在指定日期后,是否在指定日期前

    var tomorrow = new Date().add(1).day(); 
    Date.today().isAfter(tomorrow); // false
    Date.today().isBefore(tomorrow); // true
    
    var yesterday = new Date().add(-1).day();
    Date.today().isAfter(yesterday); // true
    Date.today().isBefore(yesterday); // false
    
    // No date to compare to...     
    Date.today().isAfter(); // false
    Date.today().isBefore(); // true
    
  5. 获取一年中的第几天

    Date.today().getOrdinalNumber()
    
  6. 获取时区

    Date.today().getTimezone();
    
  7. 获取指定时间的UTC偏移量,'+'或者'-'

    Date.today().getUTCOffset(); // -0600
    
  8. 获取指定时间的week of year(非UTC时间),这里周一是一周的第一天,可不是周日哦

    Date.today().getWeek();
    
  9. 当前时区是否有夏令时

    Date.today().hasDaylightSavingTime();
    
  10. 判断指定时间在当前时区中是否是在夏令时

    Date.today().isDaylightSavingTime(); // true|false
    
  11. 返回上/下一个day of week,如下,假如当天是周日

    Date.today().moveToDayOfWeek(0);     // move to next Sunday
    Date.today().moveToDayOfWeek(0, -1); // move to last Sunday
    
  12. 返回当月的第一天的时间(假如当前日期是2007-10-19)

    Date.today().moveToFirstDayOfMonth(); // 1-Nov-2007
    
  13. 返回当月的最后一天的时间(假如当前日期是2007-10-19)

    new Date(2007, 10, 19).moveToLastDayOfMonth(); // 30-Nov-2007
    
  14. 返回指定时间的上一个或者下一个指定月份的时间,0代表一月,11代表十二月

    Date.today().moveToMonth(0);     // move to next January
    Date.today().moveToMonth(0, -1); // move to last January
    
  15. 设置时间

    Date.today().set({ day: 15, hour: 8 }); // Sets the day to the 15th day of the current month and the hour to 8 (AM).
    
  16. 设置时区

    Date.today().setTimezone(PST);
    
  17. 格式化日期(yyyy-MM-dd HH:mm:ss)

    Date.today().toString();           // native .toString() functionality
    Date.today().toString(M/d/yyyy); // 11/19/2007
    Date.today().toString(d-MMM-yyyy); // 19-Nov-2007
    new Date().toString(HH:mm);      // 18:45