Date formatting time in jQuery

var date = "2014-07-12 10:54:11";

How can I show this in the format of July 12, 2014 at 10:51? Is there any function like

var newDate = formatNewDate(date);

From which can I get the date when "July 12, 2014 at 10:51 a.m."?

+4
source share
5 answers
var d = new Date();
var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

var date = d.getDate() + " " + month[d.getMonth()] + ", " + d.getFullYear();
var time = d.toLocaleTimeString().toLowerCase();

console.log(date + " at " + time);
// 6 Jul, 2014 at 1:35:35 pm

Or you can have a function

var my_date_format = function(d){
    var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var date = d.getDate() + " " + month[d.getMonth()] + ", " +  d.getFullYear();
    var time = d.toLocaleTimeString().toLowerCase();
    return (date + " at " + time); 
}(new Date());

Using:

console.log(my_date_format);

2nd solution

var my_date_format = function(input){
    var d = new Date(Date.parse(input.replace(/-/g, "/")));
    var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var date = d.getDate() + " " + month[d.getMonth()] + ", " + d.getFullYear();
    var time = d.toLocaleTimeString().toLowerCase().replace(/([\d]+:[\d]+):[\d]+(\s\w+)/g, "$1$2");
    return (date + " " + time);  
};

console.log(my_date_format("2014-07-12 11:28:13"));
// output 6 Jul, 2014 11:28 am

Check jsBin

Extra note: Some date formats are not supported in all browsers!

// "2014/07/12"      -> yyyy/mm/dd [IE, FF, Chrome]
// "07-12-2014"      -> mm-dd-yyyy [IE, Chrome]
// "July 12, 2014";  -> mmmm dd, yyyy [IE, FF]
// "Jul 12, 2014";   -> mmm dd, yyyy [IE, FF]
+7
source

I created a special date string format function, you can use it.

var  getDateString = function(date, format) {
        var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        getPaddedComp = function(comp) {
            return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
        },
        formattedDate = format,
        o = {
            "y+": date.getFullYear(), // year
            "M+": months[date.getMonth()], //month
            "d+": getPaddedComp(date.getDate()), //day
            "h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
             "H+": getPaddedComp(date.getHours()), //hour
            "m+": getPaddedComp(date.getMinutes()), //minute
            "s+": getPaddedComp(date.getSeconds()), //second
            "S+": getPaddedComp(date.getMilliseconds()), //millisecond,
            "b+": (date.getHours() >= 12) ? 'PM' : 'AM'
        };

        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                formattedDate = formattedDate.replace(RegExp.$1, o[k]);
            }
        }
        return formattedDate;
    };

Now suppose you have: -

    var date = "2014-07-12 10:54:11",
    objDate = Date.parse(date.replace(/-/g, "/"));;

So, to format this date, you write: -

var formattedDate = getDateString(new Date(objDate ), "d M, y at h:m b")
+1
source

-

. . : Date . , .

:

var d = new Date('2014-07-12 10:54:11')

since some browsers will treat it like UTC, some of them are local, while others will not parse it at all. The following converts the string to the requested format without creating a Date object:

function formatDate(s){
  var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  var b = s.split(/\D+/);
  return b[2] + ' ' + months[+b[1]] + ', ' + b[0] + ' at ' + (b[3]%12 || 12) +
         ':' + b[4] + ' ' + (b[3] < 12? 'am':'pm');
}

However, if you need to create a Date object for any other reason, the following may help:

function formatDate(s){
  function z(n){return (n<10?'0':'')+n}
  var months = ['Jan','Feb','Mar','Apr','May','Jun',
                'Jul','Aug','Sep','Oct','Nov','Dec'];
  var b = s.split(/\D+/);
  var d = new Date(b[0],--b[1],b[2],b[3],b[5],b[5]);
  return d.getDate() + ' ' + months[d.getMonth()] + ', ' + d.getFullYear() +
         ' at ' + z(d.getHours()%12 || 12) + ':' + z(d.getMinutes()) +
         ' ' + (d.getHours() < 12? 'am':'pm');
}
0
source
function convertMysqldate(dateStr) {    // Assuming input:2014-01-30 16:21:09
            var t = dateStr.split(/[- :]/);
            var monthNames = ["January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"];
            var year = t[0];
            var month = monthNames[parseInt(t[1])];
            var day = t[2];
            var hourTmp = t[3];
            var minute = t[4];
            var seconds = t[5];
            if (parseInt(hourTmp) > 12) {
                var hour = parseInt(parseInt(hourTmp) – 12) + ‘:’ + minute + ‘:’ + seconds + ‘ PM’;
            } else if (parseInt(hourTmp) === 12) {
                hour = parseInt(hourTmp) + ‘:’ + minute + ‘:’ + seconds + ‘ PM’;
            } else {
                hour = parseInt(hourTmp) + ‘:’ + minute + ‘:’ + seconds + ‘ AM’;
            }
            return (hour + ‘<br>’ + day + ‘ ‘ + month + ‘ ‘ + year);
        }

Copied here

MySql Date Formatting

0
source

Source: https://habr.com/ru/post/1548173/


All Articles