cari

Rumah  >  Soal Jawab  >  teks badan

javascript - js, penukaran tarikh gagal

Saya guna

var time = new Date("17 五月 2017 14:01:59")

Ralat penukaran, tetapi menggunakan

var time = new Date("2017-05-17 14:01:59")

Penukaran berjaya.
Jika saya perlu menukar menggunakan format tarikh berikut:

17 五月 2017 14:01:59  

Bagaimana cara mengatasinya?

阿神阿神2793 hari yang lalu720

membalas semua(7)saya akan balas

  • 大家讲道理

    大家讲道理2017-05-18 10:49:30

    const dateStr = '17 五月 2017 14:01:59'
    
    function convert2ISO8061 (dateStr) {
      const units = dateStr.split(' ')
      return units[2] + '-' + convertMonth(units[1]) + '-' + units[0] + 'T' + units[3] + 'Z'
    }
    
    function convertMonth (mStr) {
      const months = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
      const month = months.indexOf(mStr) + 1
      if (month < 10) {
        return '0' + month
      }
      return parseInt(month)
    }
    
    console.log(Date.parse(convert2ISO8061(dateStr)))
    

    balas
    0
  • 阿神

    阿神2017-05-18 10:49:30

    Tukar dahulu "17 Mei 2017 14:01:59" kepada arr tatasusunan menggunakan split(' '). "17 五月 2017 14:01:59" 使用 split(' ') 转换为数组 arr。

    再写一个函数将汉字 五月

    Tulis fungsi lain untuk menukar aksara Cina Mei ke dalam bentuk angka, dan kemudian gunakan arr tatasusunan untuk mengulang dan menggabungkannya semula ke dalam format tarikh yang anda perlukan. 🎜

    balas
    0
  • 迷茫

    迷茫2017-05-18 10:49:30

    var time = new Date("17 Mei 2017 14:01:59"); Tidak mengapa; .

    balas
    0
  • 習慣沉默

    習慣沉默2017-05-18 10:49:30

    Anda perlu melakukan semula Mei sendiri dan kemudian sambungkannya

    balas
    0
  • PHP中文网

    PHP中文网2017-05-18 10:49:30

    Saya salah faham maksud tadi, tetapi boleh ditulis begini mengikut idea di atas

    function converDate(date){
      var arr=date.split(' ');
      var ENdate=['Jan', 'Feb', 'Mar', 'April', 'May', 'Jan', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
      var CNdate=['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
      var idx=CNdate.indexOf(arr[1]);
      arr[1]=ENdate[idx];
      return new Date(arr.join(' '));
    }
    converDate('17 五月 2017 14:01:59');

    balas
    0
  • 仅有的幸福

    仅有的幸福2017-05-18 10:49:30

    DateUtils = {
        format : function(date, format) {
            if (date == null || date.length == 0)
                return '';
    
            if (typeof(date) == 'number')
                date = new Date(date);
            
            var dayArray = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
            
            var o = {
                "M+" : date.getMonth() + 1,
                "d+" : date.getDate(),
                "H+" : date.getHours(),
                "m+" : date.getMinutes(),
                "s+" : date.getSeconds(),
                "q+" : Math.floor((date.getMonth() + 3) / 3),
                "S" : date.getMilliseconds(),
                "day" : dayArray[date.getDay()]
            };
    
            if (/(y+)/.test(format))
                format = format.replace(RegExp., (date.getFullYear() + "").substr(4 - RegExp..length));
    
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    if (k == "day")
                        format = format.replace(RegExp., o[k]);
                    else
                        format = format.replace(RegExp., RegExp..length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                }
            }
    
            return format;
        },
        
        parse : function(string, format) {
            if (format == null || format.length == 0) {
                var time = Date.parse(string);
                return new Date(time);
            }
        
            if (!DateUtils.isValidate(string, format))
                return new Date();
        
            var regexp = DateUtils.regexp(format);
            var index = DateUtils.validateIndex(format);
        
            var dateArray = regexp.exec(string);
        
            var now = new Date();
            var year = (index[0] >= 0) ? dateArray[index[0] + 1] : now.getFullYear();
            var month = (index[1] >= 0) ? (dateArray[index[1] + 1] - 1) : now.getMonth();
            var day = (index[2] >= 0) ? dateArray[index[2] + 1] : now.getDate();
            var hour = (index[3] >= 0) ? dateArray[index[3] + 1] : "";
            var minute = (index[4] >= 0) ? dateArray[index[4] + 1] : "";
            var second = (index[5] >= 0) ? dateArray[index[5] + 1] : "";
        
            var date = (hour == "") ? new Date(year, month, day) : new Date(year, month, day, hour, minute, second);
            if (date.getDate() == day)
                return date;
        
            return new Date();
        },
        
        display : function(dateString) {
            if (dateString == null || dateString.length == 0)
                return '';
            
            var format = 'yyyyMMdd';
            if (dateString.length == format.length) {
                var date = DateUtils.parse(dateString, format);
                return DateUtils.format(date, 'yyyy-MM-dd');
            }
            
            format = 'yyyyMMddHHmmss';
            if (dateString.length == format.length) {
                var date = DateUtils.parse(dateString, format);
                return DateUtils.format(date, 'yyyy-MM-dd HH:mm:ss');
            }
            
            return dateString;
        },
        
        trimDisplay : function(dateString) {
            if (dateString == null || dateString.length == 0)
                return '';
            
            return dateString.replace(/[^0-9]+/g, '');
        },
    
        addDays : function(date, amount) {
            var time = 1000 * 60 * 60 * 24 * amount;
            return new Date(date.getTime() + time);
        },
        
        secondToTimeString : function(second) {
            if (second == null || second.length == 0)
                return '';
            
            if (second < 60)
                return '00:00:' + DateUtils.toFormatString(second, 2, '0');
            else if (second < 3600) {
                var minute = parseInt(second / 60);
                return '00:' + DateUtils.toFormatString(minute, 2, '0') + ':' + DateUtils.toFormatString(second % 60, 2, '0');
            } else if (second < 86400) {
                var hour = parseInt(second / 3600);
                var minute = parseInt(second % 3600 / 60);
                return DateUtils.toFormatString(hour, 2, '0') + ':' + DateUtils.toFormatString(minute, 2, '0') + ':' + DateUtils.toFormatString(second % 60, 2, '0');
            } else {
                var day = parseInt(second / 86400);
                var hour = parseInt(second % 86400 / 3600);
                var minute = parseInt(second % 86400 % 3600 / 60);
                return day + '.' + DateUtils.toFormatString(hour, 2, '0') + ':' + DateUtils.toFormatString(minute, 2, '0') + ':' + DateUtils.toFormatString(second % 60, 2, '0');
            }
        },
    
        toFormatString : function(value, length, missValue) {
            var string = value + '';
            if (string.length >= length) {
                return string;
            } else {
                while (string.length < length)
                    string = missValue + string;
                
                return string;
            }
        },
        
        regexp : function(format) {
            var y4 = "([0-9]{4})";
            var y2 = "([0-9]{2})";
            var M2 = "(0[1-9]|1[0-2])";
            var M1 = "([1-9]|1[0-2])";
            var d2 = "(0[1-9]|[1-2][0-9]|30|31)";
            var d1 = "([1-9]|[1-2][0-9]|30|31)";
            var H2 = "([0-1][0-9]|20|21|22|23)";
            var H1 = "([0-9]|1[0-9]|20|21|22|23)";
            var m2 = "([0-5][0-9])";
            var m1 = "([0-9]|[1-5][0-9])";
            var s2 = "([0-5][0-9])";
            var s1 = "([0-9]|[1-5][0-9])";
        
            var reg = format;
            reg = reg.replace(/yyyy/, y4);
            reg = reg.replace(/yy/, y2);
            reg = reg.replace(/MM/, M2);
            reg = reg.replace(/M/, M1);
            reg = reg.replace(/dd/, d2);
            reg = reg.replace(/d/, d1);
            reg = reg.replace(/HH/, H2);
            reg = reg.replace(/H/, H1);
            reg = reg.replace(/mm/, m2);
            reg = reg.replace(/m/, m1);
            reg = reg.replace(/ss/, s2);
            reg = reg.replace(/s/, s1);
        
            return new RegExp("^" + reg + "$");
        },
        
        isValidate : function(string, format) {
            if (string == null || string.length == 0)
                return false;
        
            var trimString = string.replace(/(^\s*)|(\s*$)/g, "");
            if (trimString == null || trimString.length == 0)
                return false;
        
            return DateUtils.regexp(format).test(string);
        },
        
        validateIndex : function(format) {
            var array = new Array();
            var i = 0, j = 0;
        
            var yi = format.search(/yyyy/);
            if (yi < 0)
                yi = format.search(/yy/);
            if (yi >= 0) {
                array[i] = yi;
                i++;
            }
        
            var Mi = format.search(/MM/);
            if (Mi < 0)
                Mi = format.search(/M/);
            if (Mi >= 0) {
                array[i] = Mi;
                i++;
            }
        
            var di = format.search(/dd/);
            if (di < 0)
                di = format.search(/d/);
            if (di >= 0) {
                array[i] = di;
                i++;
            }
        
            var Hi = format.search(/HH/);
            if (Hi < 0)
                Hi = format.search(/H/);
            if (Hi >= 0) {
                array[i] = Hi;
                i++;
            }
        
            var mi = format.search(/mm/);
            if (mi < 0)
                mi = format.search(/m/);
            if (mi >= 0) {
                array[i] = mi;
                i++;
            }
        
            var si = format.search(/ss/);
            if (si < 0)
                si = format.search(/s/);
            if (si >= 0) {
                array[i] = si;
                i++;
            }
        
            var resultArray = new Array(yi, Mi, di, Hi, mi, si);
        
            for (i = 0; i < array.length - 1; i++) {
                for (j = 0; j < array.length - 1 - i; j++) {
                    if (array[j] <= array[j + 1])
                        continue;
        
                    var temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        
            for (i = 0; i < array.length; i++) {
                for (j = 0; j < resultArray.length; j++) {
                    if (array[i] != resultArray[j])
                        continue;
        
                    resultArray[j] = i;
                }
            }
        
            return resultArray;
        }
    };
    var monthObject = {
        '一月' : '01',
        '二月' : '02',
        '三月' : '03',
        '四月' : '04',
        '五月' : '05',
        '六月' : '06',
        '七月' : '07',
        '八月' : '08',
        '九月' : '09',
        '十月' : '10',
        '十一月' : '11',
        '十二月' : '12'
    }
    var string = '17 五月 2017 14:01:59';
    var keys = Object.keys(monthObject);
    for (var i = 0; i < keys.length; i++) {
        var key = keys[i];
        string = string.replace(key, monthObject[key]);
    }
    console.log(string);
    
    var date = DateUtils.parse(string, 'dd MM yyyy HH:mm:ss');
    console.log(date);
    console.log(DateUtils.format(date, 'yyyy-MM-dd HH:mm:ss'));

    balas
    0
  • 阿神

    阿神2017-05-18 10:49:30

    moment.js

    balas
    0
  • Batalbalas