javascript时间戳和日期字符串相互转换代码(超简单)

作者:jingxian 时间:2024-04-30 10:09:20 

javascript时间戳和日期字符串相互转换代码(超简单)


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);

// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872
console.log(stringTime + "的时间戳为:" + timestamp2);

// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toGMTString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toJSON());
// 2014年6月18日
console.log(newDate.toLocaleDateString());
// 2014年6月18日 上午10:33:24
console.log(newDate.toLocaleString());
// 上午10:33:24
console.log(newDate.toLocaleTimeString());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toUTCString());

Date.prototype.format = function(format) {
   var date = {
      "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 (/(y+)/i.test(format)) {
      format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
   }
   for (var k in date) {
      if (new RegExp("(" + k + ")").test(format)) {
          format = format.replace(RegExp.$1, RegExp.$1.length == 1
             ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
      }
   }
   return format;
}
console.log(newDate.format('yyyy-MM-dd h:m:s'));

</script>
标签:js,日期,字符串,时间戳
0
投稿

猜你喜欢

  • Python代码模拟CPU工作原理

    2023-08-04 15:23:49
  • PHP行为设计模式之策略模式

    2023-05-28 08:34:46
  • 跟老齐学Python之重回函数

    2022-06-29 16:55:43
  • python中正则表达式findall的用法实例

    2022-02-24 07:51:28
  • Python利用xlrd 与 xlwt 模块操作 Excel

    2022-07-19 20:57:13
  • 使用Python下的XSLT API进行web开发的简单教程

    2022-07-24 22:07:14
  • Python Asyncio 库之同步原语常用函数详解

    2021-04-27 03:50:39
  • Python判断文件和文件夹是否存在的方法(最新推荐)

    2022-06-08 02:53:32
  • js获取地址栏中传递的参数(两种方法)

    2024-04-17 09:54:30
  • python实现实时视频流播放代码实例

    2021-09-11 21:11:22
  • Go 热加载之fresh详解

    2024-03-23 14:27:26
  • PyTorch CNN实战之MNIST手写数字识别示例

    2021-09-06 15:20:21
  • vant之van-list的使用及踩坑记录

    2023-07-02 16:48:41
  • Python wxPython库Core组件BoxSizer用法示例

    2023-01-17 23:08:32
  • 微信小程序实现日期格式化

    2023-07-20 20:28:32
  • Python使用Tkinter实现滚动抽奖器效果

    2023-04-07 03:05:45
  • Python之二维正态分布采样置信椭圆绘制

    2021-04-08 06:39:09
  • 如何查看access数据库中各元素的最大容量

    2007-08-28 12:44:00
  • sql 查询慢的原因分析

    2024-01-16 13:11:29
  • js控制文本框输入的字符类型方法汇总

    2024-04-10 13:57:03
  • asp之家 网络编程 m.aspxhome.com