web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

js如何获取时间戳并转换成日期

404 ℃
     

js如何获取当前时间戳并转换成日期,下面web建站小编给大家详细介绍一下!

转换代码如下:

function formatDateTime(int, str, bol = true) {
  let date = new Date(int);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hours = date.getHours();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();
  if (bol) {
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    hours = hours < 10 ? '0' + hours : hours
    minutes = minutes < 10 ? '0' + minutes : minutes
    seconds = seconds < 10 ? '0' + seconds : seconds
  }

  //把传入的转换成小写
  let strtoLowerCase = str.toLowerCase(); 

  //str传值类型
  switch (strtoLowerCase) {
    case 'y-m-d h:i:s':
      return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
      break;
    case 'ymdhis':
      return year + '' + month + '' + day + '' + hours + '' + minutes + '' + seconds;
      break;
    case 'y-m-d':
      return year + '-' + month + '-' + day;
      break;
    case 'y-m':
      return year + '-' + month;
      break;
    case 'm-d':
      return month + '-' + day;
      break;
    case 'ymd':
      return year + '' + month + '' + day;
      break;
    case 'h:i:s':
      return hours + ':' + minutes + ':' + seconds;
      break;
    case 'his':
      return hours + '' + minutes + '' + seconds;
      break;
    case 'y年m月d日 h时i分s秒':
      return year + '年' + month + '月' + day + '日' + ' ' + hours + '时' + minutes + '分' + seconds + '秒';
      break;
    case 'y年m月d日':
      return year + '年' + month + '月' + day + '日';
      break;
    case 'h时i分s秒':
      return hours + '时' + minutes + '分' + seconds + '秒';
      break;
    case 'y年m月':
      return year + '年' + month + '月';
      break;
    case 'm月d日':
      return month + '月' + day + '日';
      break;
  }
}

输出结果:

console.log("转换结果: "+formatDateTime(new Date(), 'y年m月d日', bol = true))
//转换结果: 2023年01月27日

mysql语法中有哪些获取当前时间方法

wordpress如何把发布的具体时间改成(几分/几小时前)类型

如何利用php语法将日期转成礼拜

php语法如何查询指定时间段内的数据

php如何实现时间转换?(时间戳)

标签: 时间转换, 获取时间

上面是“js如何获取时间戳并转换成日期”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_3418.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > js
本文共计1318个字,预计阅读时长9分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款简单高效漂亮的多平台笔记应用——Zoho Notebook
下一篇: 推荐2款充满科技感的艺术字体——小新黑体、小新潮酷体
x 打工人ai神器