Skip to content

Instantly share code, notes, and snippets.

@LulzAugusto
Last active August 29, 2015 14:03
Show Gist options
  • Save LulzAugusto/8bf8d51ce7381e33f39d to your computer and use it in GitHub Desktop.
Save LulzAugusto/8bf8d51ce7381e33f39d to your computer and use it in GitHub Desktop.
A simple date handler that does the job while we don't need something complex as Moment.js
/*=====================================================
*
* Sitef Date : A javascript library that handles dates in a way that fits Sitef's needs
* (c) Luiz Augusto Crisostomo 2014
*
======================================================*/
function SitefDate(date) {
/* _ Object Constructor
========================*/
if (window === this) {
return new SitefDate();
}
if (date != undefined) {
if (typeof(date) === 'number') {
this.date = new Date(date);
} else if (date instanceof Date) {
this.date = date;
} else if (typeof date === 'string') {
this.date = new Date(stringToDate(date));
} else {
throw 'You MUST use a Date object, a number or a ISO date string as parameter of the constructor';
}
} else {
this.date = new Date();
}
this.about = {
version: '1.0.1',
author: 'Luiz Augusto',
created: 'June 2014',
updated: '26 June 2014',
company: 'Inform Sistemas Ltda.'
};
/* _ Prototype Functions
============================*/
this.getDateString = function() {
return zeroInFrontOf(self.date.getDate()) + '/' + zeroInFrontOf(self.date.getMonth() + 1) + '/' + this.date.getFullYear();
};
this.getTimeString = function() {
return zeroInFrontOf(this.date.getHours()) + ':' + zeroInFrontOf(this.date.getMinutes());
};
this.getISODate = function() {
return this.date.getFullYear() + '-' + zeroInFrontOf(this.date.getMonth() + 1) + '-' + zeroInFrontOf(this.date.getDate());
};
/* _ Helper Functions
============================*/
var self = this;
function zeroInFrontOf(param) {
if (param < 10) {
return '0' + param;
}
return param;
}
function stringToDate(string) {
var slices = string.split('/');
return new Date(slices[2], slices[1]-1, slices[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment