数据库查询时间转换的问题。
发布于 5 年前 作者 renjie 14794 次浏览 来自 问答

向数据库查询信息,其中返回给小程序的时间字段:

是这样。

数据库中:

js 中没有SimpleDateFormat 怎么处理转换。

大佬解答下。😮😀

3 回复
/** 扩展Date对象方法:格式化日期 */
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
};

使用方法:

new Date(返回的时间).format();

如果不传参数默认格式化成:yyyy-MM-dd hh:mm:ss格式

像这种问题一搜一大把,有提问这个时间早就研究出来了。

过来学习,谢谢分享。

有Date类呀   你可以自己写转换或者去百度一个

回到顶部