时间字符串转中国标准时间转时间戳转年月日格式

1.将一个时间转年月日时分秒

  (1) 普通字符串时间可转为中国标准时间

       let now = new Date("2020-12-12 13:00");  得到:Sat Dec 12 2020 13:00:00 GMT+0800 (中国标准时间)

  (2) 转为时间戳

       let date=now.getTime();  得到 1607749200000

  (3) 转为 年月日时分秒

       得到:2020年12月12日 13点00分00秒

      let time = "2020-12-12 13:00";
      let now = new Date(time); //Sat Dec 12 2020 13:00:00 GMT+0800 (中国标准时间)
      let date=now.getTime();  // 时间戳 1607749200000
      // 获取年
      let yy = now.getFullYear();
      // 获取月
      let mm = now.getMonth() + 1;
      // 获取日
      let dd = now.getDate();
      // h获取小时
      let hh = now.getHours();
      // 获取分钟
      let min = now.getMinutes();
      // 获取秒
      let ss = now.getSeconds();
      hh = hh < 10 ? "0" + hh : hh;
      min = min < 10 ? "0" + min : min;
      ss = ss < 10 ? "0" + ss : ss;
      let date= `${yy}年${mm}月${dd}日 ${hh}点${min}分${ss}秒`
      console.log(date)  // 2020年12月12日 13点00分00秒

2.求已过去x天x时x分x秒

现在时间的时间戳减之前时间时间戳进行计算

    let  date = "2020-12-12 13:00"; //给的之前时间
    let beforeTime = new Date(date).getTime(); //之前时间戳 1607749200000
    let nowTime = new Date().getTime();  // 现在的时间戳 1648020150786
    let time = nowTime - beforeTime; // 差别的时间戳 40271370582
		let day=Math.floor(time/(1000*60*60*24))
		let hours=Math.floor((time/(1000*60*60))%24)
		let minutes=Math.floor((time/(1000*60))%60)
		let seconds=Math.floor((time/1000)%60)
    let formerlyTime=day+'天'+hours+'小时'+minutes+'分钟'+seconds+'秒'
     //已过去466天2小时31分钟39秒
    console.log('已过去'+ formerlyTime) 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将JavaScript中的时间换为年月格式,可以使用以下代码: ```javascript // 获取当前时间 let timestamp = Date.now(); // 将时间换为Date对象 let date = new Date(timestamp); // 获取年、月、 let year = date.getFullYear(); let month = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); let day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); // 拼接成年月格式 let formattedDate = year + '-' + month + '-' + day; console.log('输出内容:', formattedDate); ``` 这段代码首先获取当前时间,然后使用`new Date(timestamp)`将时间换为Date对象。接着,通过`getFullYear()`、`getMonth()`和`getDate()`方法获取年、月、,并使用三元运算符和字符串拼接将它们格式化为年月的形式。最后,将格式化后的期打印出来。 #### 引用[.reference_title] - *1* [js获取时间,将时间换为年月时分秒](https://blog.csdn.net/Aom_yt/article/details/119825165)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [js 时间换,标准时间转年时分秒(补0),时间](https://blog.csdn.net/jmhaoxuan/article/details/127609186)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值