有关时间戳的方法

本文介绍了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'));

将时间戳转换为字符串
function timestampToString(timstamp) {
    var newDate = new Date();
    newDate.setTime(timstamp * 1000);
    var month = (newDate.getMonth() + 1);
    var date = newDate.getDate();
    var hours = newDate.getHours();
    var minutes = newDate.getMinutes();
    var seconds = newDate.getSeconds();
    return newDate.getFullYear() + "-" + (month < 10 ? "0" : "") + month + "-"
        + (date < 10 ? "0" : "") + date + " "
        + (hours < 10 ? "0" : "") + hours + ":"
        + (minutes < 10 ? "0" : "") + minutes + ":"
        + (seconds < 10 ? "0" : "") + seconds;
}
</script>


JS获取当前时间的方法

Js获取当前日期时间及其它操作

var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间

日期时间脚本库方法列表

Date.prototype.isLeapYear 判断闰年
Date.prototype.Format 日期格式化
Date.prototype.DateAdd 日期计算
Date.prototype.DateDiff 比较日期差
Date.prototype.toString 日期转字符串
Date.prototype.toArray 日期分割为数组
Date.prototype.DatePart 取日期的部分信息
Date.prototype.MaxDayOfDate 取日期所在月的最大天数
Date.prototype.WeekNumOfYear 判断日期所在年的第几周
StringToDate 字符串转日期型
IsValidDate 验证日期有效性
CheckDateTime 完整日期时间检查
daysBetween 日期天数差

### JavaScript 中将日期转换为时间戳方法 在 JavaScript 中,可以通过多种方法将日期对象或字符串转换为时间戳。以下是几种常见的方式: #### 方法一:使用 `Date` 对象的 `getTime()` 方法 创建一个 `Date` 对象后,可以调用其内置的 `getTime()` 方法来获取对应的时间戳(单位为毫秒)。如果需要以秒为单位的时间戳,则可将结果除以 1000 并取整[^3]。 ```javascript let date = new Date(); let timestampInMilliseconds = date.getTime(); // 获取毫秒级时间戳 let timestampInSeconds = Math.floor(timestampInMilliseconds / 1000); // 转换为秒级时间戳 console.log("毫秒级时间戳:", timestampInMilliseconds); console.log("秒级时间戳:", timestampInSeconds); ``` #### 方法二:利用 `valueOf()` 方法 除了 `getTime()` 外,还可以使用 `valueOf()` 方法直接返回时间戳值,功能与前者相同[^3]。 ```javascript let date = new Date(); let timestamp = date.valueOf(); // 返回毫秒级时间戳 console.log("时间戳:", timestamp); ``` #### 方法三:借助 `Date.parse()` 解析日期字符串 对于给定的日期字符串,可通过静态方法 `Date.parse()` 进行解析并得到相应的时间戳(单位仍为毫秒)。此方法适用于标准格式如 ISO 8601 的日期串[^3]。 ```javascript let dateString = "2025-05-20T00:00:00Z"; let parsedTimestamp = Date.parse(dateString); if (!isNaN(parsedTimestamp)) { console.log("解析成功的時間戳:", parsedTimestamp); } else { console.error("无法解析指定的日期字符串"); } ``` > **注意事项**: 当处理来自用户的输入或者不确定来源的数据时,务必先确认其合法性再执行后续操作以防引发异常情况发生[^4]。 --- ### 总结 综上所述,在 JavaScript 编程语言里实现从具体某一天到它所代表的那个瞬间之间跨越了多少个千分之一秒钟这一过程非常简单明了——无论是通过实例化新的日期类还是单纯依靠已有API都能轻松达成目标[^5]。每种途径各有优劣之处,开发者应根据实际需求灵活选取最合适的方案加以运用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值