web建站教程
     
  1. 首页
  2. 前端UI组件库
  3. AI项目和框架
  4. AIGC工具
  5. 百度echarts
  6. 地图大全
  7. 前端知识
  8. 更多
    vuejs
    js入门
    php入门
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    AI应用

js封装获取当前时间代码

748 ℃

js如何获取当前时间代码,下面web建站小编给大家详细介绍一下具体代码!

具体代码如下:

function currentDate(type, isno) {
  var date = new Date();
  var year = singular(date.getFullYear());
  var month = singular(date.getMonth() + 1);
  var strDate = singular(date.getDate());
  var hours = singular(date.getHours());
  var minutes = singular(date.getMinutes());
  var seconds = singular(date.getSeconds());
  if (type) {
    if (isno) {
      return year + "年" + month + "月" + strDate + "日 " + hours + "时" + minutes + "分" + seconds + "秒";
    } else {
      return year + "年" + month + "月" + strDate + "日";
    }
  } else {
    if (isno) {
      return year + "-" + month + "-" + strDate + " " + hours + ":" + minutes + ":" + seconds;
    } else {
      return year + "-" + month + "-" + strDate;
    }
  }
}

单数补位

function singular(num) {
  if (num < 10) {
    num = "0" + num;
  }
  return num;
}

方法调用

console.log('输出结果:', currentDate(true, true))
//输出结果:2023年01月11日 12时59分55秒

console.log('输出结果:', currentDate(true, false))
//输出结果:2023年01月11日

console.log('输出结果:', currentDate(false, true))
//输出结果:2023-01-11 13:02:40

console.log('输出结果:', currentDate(false, false))
//输出结果:2023-01-11

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

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

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

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

php语法如何转换发布时间(刚刚/几分钟前/几小时前)

标签: 当前时间 获取时间

上面是“js封装获取当前时间代码”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

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

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

当前位置: 首页 > js
Trae:新一代免费的AI编程工具

呱呱工具箱

一款免费的在线小工具,无需下载,打开即可使用!呱呱工具箱

在线育儿补贴计算器

快来看看你到底可以领到多少补贴!生活小工具
上一篇:
下一篇:
x 打工人ai神器