Skip to content

Instantly share code, notes, and snippets.

@kovaldn
Last active December 24, 2015 17:49
Show Gist options
  • Save kovaldn/5590738 to your computer and use it in GitHub Desktop.
Save kovaldn/5590738 to your computer and use it in GitHub Desktop.
Javascript: date
/*work with Date*/
/*
* DATA.JS
* https://code.google.com/p/datejs/wiki/APIDocumentation#between
* http://www.datejs.com/
*/
Date.today();
Date.compare(today, future); // 1, 0, -1
Date.today().compareTo(past); // 1, 0, -1
var tomorrow = new Date().add(1).day();
Date.today().isAfter(tomorrow); // false
Date.today().isBefore(tomorrow); // true
var d1 = Date.today();
var d3 = new Date().clearTime();
Date.equals(d1, d3); // true
Date.getDayNumberFromName('Tuesday'); // 2
Date.getMonthNumberFromName('July'); // 6
Date.getDaysInMonth(2007, 10); // 30
Date.parse("today")
Date.parseExact("10/15/2004", "M/d/yyyy"); // The Date of 15-Oct-2004
new Date(2007, 10, 19).moveToFirstDayOfMonth(); // 1-Nov-2007
new Date(period).toString("MMM - yyyy"); // !!!
new Date('06').toString("MMM"); // июнь
/*
* native js
* http://www.w3schools.com/jsref/jsref_obj_date.asp
*/
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth()+1, //January is 0!
yyyy = today.getFullYear();
if(dd<10){dd='0'+dd}; // 01, 02, 03 ... 09
if(mm<10){mm='0'+mm};
today = dd+'/'+mm+'/'+yyyy;
document.write(today);
/*
* Проблеммы в IE
* Для IE в некоторых случаях нужно сначала создавать объект new Date( year, month, day);
* и только потом применять методы типа toString("MMM");
*/
var arr = thisEl.text().split('.');
day = arr[0],
month = arr[1]-1,
year = arr[2],
dateObj = new Date( year, month, day);
mmm = dateObj.toString("MMM");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment