Small JavaScript DateTimeTools object

So I got the project working on a tv schedule at work. As you can imagine there is work that needs to be done with dates and times when moving forward / back in the schedule. I found a few things out there that were full blown date/time js libraries, but they were a bit more than what I needed and didn’t do exactly what I needed. So I threw the following together. It does have specific stuff in it that is probably not portable to other projects, but here it is.

var dateTimeTools = (function ()
{
    var me = {};
 
    me.getMonthString = function (month)
    {
        var today = new Date();
        var montharray = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        month = month || today.getMonth() + 1;
 
        return montharray[month - 1];
    };
 
    me.getWeekdayString = function (weekday)
    {
        var today = new Date();
        var dayarray = ['Sun.', 'Mon.', 'Tues.', 'Weds.', 'Thurs.', 'Fri.', 'Sat.'];
        weekday = weekday || today.getDay();
 
        return dayarray[weekday];
    };
 
 
    me.getDateString = function (theDate)
    {
        theDate = theDate || new Date();
        var year, month, date, weekday;
 
        // theDate can be either an instance of a Date object, or one of the custom date startDateTime objects used elsewhere
        if (theDate.hasOwnProperty('month'))
        {
            // dealing w/ a custom startDateTime object
            year = theDate.year;
            month = me.getMonthString(theDate.month);
            date = parseInt(theDate.date, 10);
            weekday = me.getWeekdayString(theDate.weekday);
        }
        else
        {
            // dealing w/ a JS Date object
            year = theDate.getFullYear();
            month = me.getMonthString(theDate.getMonth() + 1);
            date = theDate.getDate();
            weekday = me.getWeekdayString(theDate.getDay());
        }
 
        return weekday + ', ' + month + ' ' + date + ', ' + year;
    };
 
 
    me.getOffsetTime = function (startDateTime, offset)
    {
        offset = offset || 0;
 
        if (offset !== 0)
        {
            // this isn't going to be very portable, requires page.startDateTime object on page to work
            // returns a page.startDateTime object as well
            var datetime = new Date();
            datetime.setFullYear(
                    startDateTime.year,
                    parseInt(startDateTime.month, 10),
                    parseInt(startDateTime.date, 10)
            );
 
            datetime.setHours(
                    parseInt(startDateTime.hour, 10),
                    parseInt(startDateTime.minute, 10)
            );
 
            datetime.setHours(datetime.getHours() + offset);
 
            var month = datetime.getMonth();
            var date = datetime.getDate();
            var year = datetime.getFullYear();
            var hour = datetime.getHours();
            var minute = datetime.getMinutes();
 
            // pad everything w/ zeroes
            month = dateTimeTools.getPaddedMonth(month);
            date = dateTimeTools.getPaddedDate(date);
            hour = dateTimeTools.getPaddedHour(hour);
            minute = dateTimeTools.getPaddedMinute(minute);
            minute = minute < 30 ? '00' : '30';
 
            return {
                month : month,
                date : date,
                year : year,
                hour : hour,
                minute : minute,
                time : hour + ':' + minute + ':00'
            };
        }
 
        return startDateTime;
    };
 
 
    me.getPaddedDate = function (date)
    {
        var today = new Date();
        date = date.toString() || today.getDate();
 
        return date < 10 ? '0' + date : date;
    };
 
    me.getPaddedHour = function (hour)
    {
        var today = new Date();
        hour = hour.toString() || today.getHours();
 
        return hour < 10 ? '0' + hour : hour;
    };
 
    me.getPaddedMinute = function (minute)
    {
        var today = new Date();
        minute = minute.toString() || today.getMinutes();
 
        return minute < 10 ? '0' + minute : minute;
    };
 
    me.getPaddedMonth = function (month)
    {
        var today = new Date();
        month = month.toString() || today.getMonth() + 1;
 
        return month < 10 ? '0' + month : month;
    };
 
    me.getTwelveHourTime = function (hour, minute)
    {
        var today = new Date();
        hour = hour.toString() || today.getHours(); // using toString() since 0 will eval to false
        minute = minute.toString() || today.getMinutes();
 
        // manipulate the hour to account for midnight and noon
        var suffix = (hour >= 12 && hour < 24) ? 'PM' : 'AM';
        hour = hour >= 25 ? hour - 24 : hour;
        hour = hour === '0' ? 12 : hour;
 
        return hour > 12 ? (hour - 12) + ':' + minute + ' ' + suffix : hour + ':' + minute + ' ' + suffix;
    };
 
    return me;
}());

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">