如何在javascript中将日期一个格式转换为另一个格式?

时间:2017-05-25 11:47:09

标签: javascript jquery

你能告诉我如何在javascript中将日期格式转换为另一种吗?

var date = new Date("24 May 2017, 05:35");
d=date.getDate();
m=date.getMonth();
y=date.getYear();
h=date.getHours();
m=date.getMinutes();
console.log(m +' '+d +' '+y+', ' + h+m)


// expected output
//May 24, 2017, 05.35 AM IST

https://jsfiddle.net/bbbnxfz8/

我不想使用像 moment

这样的库

3 个答案:

答案 0 :(得分:0)

例如,如果你有

var date = new Date("24 May 2017, 05:35");
var day1 = date.getDate();

您可以像这样转换 - >

var date2 = new Date(date);
var day = date2.getDay();

您可以传递始终满足日期格式的对象

你喜欢这个吗?

答案 1 :(得分:0)

// Creating Date object
var date = new Date("24 May 2017, 05:35");

// fetching date
d  =  date.getDate();

// fetching month
m  =  date.getMonth();

// fetching year
y  =  date.getFullYear();

// fetching hours
h  =  date.getHours();

// fetching minutes
min=  date.getMinutes();

// creating month's name array to display name of month
var monthArr = new Array();
monthArr = ['Jan','Feb','March','April','May']; // you can add all months

// Code to show time as per requirement
var ampm = h >= 12 ? 'pm' : 'am';
h = h % 12;
hours = h ? h : 12; // the hour '0' should be '12'
minutes = min < 10 ? '0'+min : min;

var strTime = hours + '.' + minutes + ' ' + ampm;

var finalDate = monthArr[m]+' '+d+','+y+', '+strTime;
console.log(finalDate);

我希望这会对你有所帮助。

答案 2 :(得分:0)

正如你所说的那样,我有一个解决方案,可以根据需要进行调整。 另请尝试Fiddle DEMO

function dateFormater(dateStr, format, separator) {
    var date = new Date(dateStr),
        formatArr = format.split('-'),
        len = formatArr.length,
        str = '',
        i,
        monthNames = ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
        ],
        getdata = function(d) {
            function getCurrentTime(d) {
                var currentTime,
                    hour = d.getHours(),
                    meridiem = hour >= 12 ? "PM" : "AM";
                return ((hour + 11) % 12 + 1) + ":" + d.getMinutes() + ' ' + meridiem;
            }
            switch (d) {
                case 'day':
                    return getDay(date.getDay);
                case 'month':
                    return monthNames[date.getMonth()];
                case 'dd':
                    return date.getDate();
                case 'mm':
                    return date.getMonth();
                case 'yy':
                    return date.getFullYear();
                case 'time':
                    return getCurrentTime(date);
                case 'zone':
                    return date.toString().match(/\(([A-Za-z\s].*)\)/)[1];
            }
        },
        getDay = function(d) {
            switch (d) {
                case 0:
                    return "Sunday";
                case 1:
                    return "Monday";
                case 2:
                    return "Tuesday";
                case 3:
                    return "Wednesday";
                case 4:
                    return "Thursday";
                case 5:
                    return "Friday";
                case 6:
                    return "Saturday";
            }
        };

    for (i = 0; i < len; i++) {
        i === len - 1 && (separator = '');
        str += getdata(formatArr[i]) + separator;
    }

    return str;
}

var format = 'month-dd-yy-time-zone';
console.log(dateFormater("24 May 2017, 05:35", format, ', ')); // May, 24, 2017, 5:35 AM, IST