zoukankan      html  css  js  c++  java
  • JS 转换日期UTC类型

    前台取到的日期类型为UTC,"yyyy-MM-dd'T'HH:mm:ss.SSS",后台接收报错如下:

    org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value 
    of type `java.util.Date` from String "2019-08-10T02:37:54.770Z": not a valid representation 
    (error: Failed to parse Date value '2019-08-10T02:37:54.770Z': Unparseable date: "2019-08-10T02:37:54.770Z");
    nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: 
    Cannot deserialize value of type `java.util.Date` from String "2019-08-10T02:37:54.770Z": 
    not a valid representation (error: Failed to parse Date value '2019-08-10T02:37:54.770Z': Unparseable date: "2019-08-10T02:37:54.770Z")
    

    究其原因为Spring无法将UTC类型的值解析为Date日期格式,解决方法如下:

    formatTime(time, format) {
            var date = new Date(time);
            var formatter = function (i) { return (i < 10 ? '0' : '') + i };
            return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
              switch (a) {
                case 'yyyy':
                  return formatter(date.getFullYear());
                  break;
                case 'MM':
                  return formatter(date.getMonth() + 1);
                  break;
                case 'mm':
                  return formatter(date.getMinutes());
                  break;
                case 'dd':
                  return formatter(date.getDate());
                  break;
                case 'HH':
                  return formatter(date.getHours());
                  break;
                case 'ss':
                  return formatter(date.getSeconds());
                  break;
              }
            })
          }
    

    调用该js方法将相应值转换为Date日期类型:

    formatTime(changeDate,'yyyy-MM-dd HH:mm:ss')
    
  • 相关阅读:
    操作正则表达式的公共类
    验证码类
    强制转化常用函数
    实现Base64编码与其它编码转换的类
    关于任务管理器踩过的坑,程序员必看!
    每次找Internet选项感到抓狂?一键打开!
    教你避过安装TensorFlow的两个坑
    HTML中id与name的通俗区别
    VS code代码对齐快捷键
    form和table的区别
  • 原文地址:https://www.cnblogs.com/maggieq8324/p/11414753.html
Copyright © 2011-2022 走看看