Created
August 4, 2014 19:32
-
-
Save timrwood/7cb72d7960d19e889673 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var momento = moment(556095600000); // 1987-08-16T07:00:00.000Z | |
var day = momento.date(); // 16 | |
var month = momento.month() + 1; // 8 | |
var year = momento.year(); // 1987 | |
var timeperiods = { | |
year : moment(year, "YYYY").toISOString(), // 1987-01-01T08:00:00.000Z | |
month : moment(month + "-" + year, "MM-YYYY").toISOString(), // 1987-08-01T07:00:00.000Z | |
day : moment(day + "-" + month + "-" + year, "DD-MM-YYYY").toISOString() // 1987-08-16T07:00:00.000Z | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var timestamp = new Date(556095600000); // 1987-08-16T07:00:00.000Z | |
var day = timestamp.getUTCDate(); // 16 | |
var month = timestamp.getMonth() + 1; // 8 | |
var year = timestamp.getFullYear(); // 1987 | |
var timeperiods = { | |
year : new Date(year).toISOString(), // 1970-01-01T00:00:01.987Z Off by 17 years | |
month : new Date(year, month).toISOString(), // 1987-09-01T07:00:00.000Z Off by 1 month | |
day : new Date(year, month, day).toISOString() // 1987-09-16T07:00:00.000Z Off by 1 month | |
} |
Updated JSperf to run more optimized versions of the code above while still getting the same final output: http://jsperf.com/moment-js-vs-native-date
Optimized moment.js version:
var momento = moment(556095600444)
var timeperiods = {
"year": momento.clone().startOf('year').toISOString(),
"month": momento.clone().startOf('month').toISOString(),
"week": momento.clone().startOf('week').toISOString(),
"day": momento.clone().startOf('day').toISOString()
}
Optimized native version:
Date.prototype.getStartOfDay = function() {
this.setHours(0, 0, 0, 0)
return this
}
Date.prototype.getStartOfWeek = function() {
if (this.getDay()) {
this.setHours(-Math.abs(this.getDay() - 1) * 24, 0, 0, 0)
} else {
this.setHours(-6 * 24, 0, 0, 0)
}
return this
}
Date.prototype.getStartOfMonth = function() {
if (this.getDate()) {
this.setHours(-Math.abs(this.getDate() - 1) * 24, 0, 0, 0)
} else {
this.setHours(0, 0, 0, 0)
}
return this
}
Date.prototype.getStartOfYear = function() {
if (this.getDayOfYear()) {
this.setHours(-Math.abs(this.getDayOfYear() - 1) * 24, 0, 0, 0)
} else {
this.setHours(0, 0, 0, 0)
}
return this
}
Date.prototype.getDayOfYear = function() {
var date = this.clone(this)
date.setMonth(0, 0)
return Math.round((this - date) / 8.64e7)
}
Date.prototype.clone = function() {
return new Date(this.getTime())
}
var date = new Date(556095600444)
var timeperiods = {
"year": date.clone().getStartOfYear().toISOString(),
"month": date.clone().getStartOfMonth().toISOString(),
"week": date.clone().getStartOfWeek().toISOString(),
"day": date.clone().getStartOfDay().toISOString()
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oooops... here's a fixed version for native