在jQuery中格式化日期时间

时间:2014-07-12 11:02:00

标签: javascript jquery

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

我如何以2014年7月12日上午10:51的格式显示此内容? 有没有像

这样的功能
var newDate = formatNewDate(date);

我可以从中获得日期时间为" 2014年7月12日上午10:51" ?

5 个答案:

答案 0 :(得分:8)

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

或者你可以有一个功能

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());

用法:

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

检查jsBin

Extra note:所有浏览器都不支持某些日期格式!

// "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]

答案 1 :(得分:2)

已经制作了自定义日期字符串格式功能,您可以使用它。

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;
    };

现在假设你&#39; vs: -

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

所以要格式化这个日期,你要写: -

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

答案 2 :(得分:0)

答案 3 :(得分:0)

  

是否有像

这样的功能

没有。你必须自己格式化。您有两个选择:解析字符串以创建Date对象并从中生成格式化字符串,或者只使用字符串的部分并重新格式化它。在这两种情况下,都假设您希望将其视为本地日期。

不要试图这样做:

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

因为有些浏览器会将其视为UTC,有些浏览器会将其视为本地浏览器,而其他浏览器则根本无法解析它。以下内容将字符串转换为请求的格式,而不创建Date对象:

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');
}

但是,如果由于某些其他原因需要创建Date对象,则以下内容可能有所帮助:

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');
}

答案 4 :(得分:0)

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);
        }

从这里复制

MySql Date formatting