Thursday 15 June 2017

DateTiime function - SharePoint Javascript

DateFunctions = window.DateFunctions || {};

DateFunctions.formatDateBySiteLocale = function (dateValue) {
    if (dateValue != null && dateValue != '' && dateValue != undefined) {
        var dt = new Date(dateValue);
        return dt.toLocaleString(_spPageContextInfo.currentCultureName);
    }
    else {
        return '';
    }
};

DateFunctions.getDateOnlyFromUTCDateTime = function (dateValue) {
    if (dateValue != null && dateValue != '' && dateValue != undefined) {
        var dt = new Date(dateValue);
        //IE has a bug that doesn't return the date in the culture format using the javascript date object as coded below.
        //return dt.toLocaleDateString(_spPageContextInfo.currentCultureName);   
        //We have no other option other than to return date using the below code
        var month = dt.getMonth() + 1;
        month = month < 10 ? ("0" + month) : month;

        var date = dt.getDate();
        date = date < 10 ? ("0" + date) : date;

        return (date + "." + month + "." + dt.getFullYear());
    }
    else {
        return '';
    }
};

DateFunctions.parseJQueryDateToReturnCompatibleDate = function (dateValue) {
    if (dateValue != null && dateValue != '' && dateValue != undefined) {
        var dt = $.datepicker.parseDate($.datepicker._defaults.dateFormat, dateValue);
        return (dt.getMonth() + 1) + "." + dt.getDate() + "." + dt.getFullYear();
    }
    else {
        return '';
    }
};