当前位置: 首页 > news >正文

获取当前时间并转换为想要的格式

转换为YYYY-MM-DD格式

function getCurrentDate() {var today = new Date();var year = today.getFullYear();var month = today.getMonth() + 1; // 月份从0开始,需要加1var day = today.getDate();return year + '-' + (month < 10 ? ('0' + month) : month) + '-' +  (day < 10 ? ('0' + day) : day);
};
const time =  this.getCurrentDate()
console.log(time);

转换为YYYY-MM-DD HH:MM:SS / YYYY.MM.DD HH:MM:SS 格式

  getCurrentTime() {const currentDate = new Date();const year = currentDate.getFullYear();const month = currentDate.getMonth() + 1; // 月份从0开始,所以需要加1const day = currentDate.getDate();const hours = currentDate.getHours();const minutes = currentDate.getMinutes();const seconds = currentDate.getSeconds();return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;//return `${year}.${month}.${day} ${hours}:${minutes}:${seconds}`;
},const time =  this.getCurrentTime()
console.log(time);

转换为YYYY年MM月DD日/YYYY.MM.DD格式

function getCurrentDateFormatted() {const currentDate = new Date();const year = currentDate.getFullYear();const month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始,需要加1,并且保证两位数const day = currentDate.getDate().toString().padStart(2, '0'); // 保证日期为两位数return `${year}年${month}月${day}日`;//YYYY.MM.DD格式 return `${year}.${month}.${day}`;
}const formattedDate = getCurrentDateFormatted();
console.log(`当前日期:${formattedDate}`);

http://www.lryc.cn/news/156864.html

相关文章:

  • 如何实现自动化测试?
  • c++中的对齐问题
  • 力扣(LeetCode)算法_C++—— 存在重复元素
  • OpenCV实现Photoshop曲线调整
  • 【探索Linux】—— 强大的命令行工具 P.8(进程优先级、环境变量)
  • 蓝牙协议栈BLE
  • 企业架构LNMP学习笔记17
  • php 获取每月开始结束时间,指定月份的开始结束时间戳
  • Docker技术入门| Part03:Dockerfile详解(Dockerfile概念、Dockerfile 指令、使用Dockerfile构建镜像)
  • 分享一个有意思的线程相关的程序运行题
  • 集合的进阶学习
  • Java真过饱和了吗?现在学Java迟了?
  • glibc2.35-通过tls_dtor_list劫持exit执行流程
  • linux-OpenSSL升级
  • Nginx全家桶配置详解
  • CMake生成Visual Studio工程
  • 数学建模--K-means聚类的Python实现
  • 防坠安全带上亚马逊美国站要求的合规标准是什么?
  • PDF转Word的方法分享与注意事项。
  • gitlab配置webhook,commit message的时候校验提交的信息
  • 借助CIFAR10模型结构理解卷积神经网络及Sequential的使用
  • Java # Java基础八股
  • 【Spring Boot】SpringBoot 2.6.6 集成 SpringDoc 1.6.9 生成swagger接口文档
  • 【算法】快速排序 详解
  • 架构师spring boot 面试题
  • 电商系统架构设计系列(十一):在电商的交易类系统中,如何正确地使用 Redis 这样的缓存系统呢?需要考虑哪些问题?
  • MySQL数据库和表的操作
  • DAY-01--分布式微服务基础概念
  • 记:一次关于paddlenlp、python、版本之间的兼容性问题
  • MyBatis配置及单表操作