将日期转换为UTC格式

时间:2014-08-16 12:29:29

标签: jquery datetime

我有这个日期:2014-08-14 16:00:00我通过代码$table_of_service->getDesiredDatetime()从数据库中检索它,但我想以此格式向用户显示此日期4-Nov -2014 04:00 PM然后计算此时间的UTC时间并将其显示给用户。

那么我将如何将日期转换为我想要的格式并计算JQuery中特定日期的UTC时间?

请注意,日期值我在php中收到它,我必须在JS中使用它。 在php中这是代码:

$ x = $ concierge_service-> getDesiredDatetime();

并且在JS中我必须获取$ x的值并将其存储在JS变量中以处理其值     var x =;

1 个答案:

答案 0 :(得分:0)

试试这个:

$(document).ready(function () {
            var datetime = '2014-08-14 16:00:00';
            var split = [];
            split = datetime.split(" ");
            var convertedDateTime = Format(split[0]) + ' ' + tConvert(split[1]);

        });



        function Format(givenDate) {
            var mydate = new Date(givenDate);
            var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][mydate.getMonth()];
            var resultDate = ("0" + mydate.getDate()).slice(-2) + '-' + month + '-' + mydate.getFullYear();

            return resultDate;
        }

        function tConvert(time) {
            // Check correct time format and split into components
            time = time.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];

            if (time.length > 1) { // If time format correct
                time = time.slice(1);  // Remove full string match value
                time[5] = +time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM
                time[0] = +time[0] % 12 || 12; // Adjust hours
            }
            return time.join(''); // return adjusted time or original string
        }

以下是 DEMO