1、js取当前时间的秒级时间戳
parseInt(new Date().getTime()/1000);,
//或
Date.parse(new Date())/1000;
2、将当前日期转换为时间戳。
(1)、当前时间的时间戳
var now = new Date();
console.log(now.getTime()) //当前时间的时间戳
// 将当前日期转换为时间戳,getTime()方法可返回距1970年1月1日之间的毫秒数。
//也可以使用 +now ,该效果等同于now.getTime()
(2)、将指定日期转换为时间戳。
var t = "2017-12-08 20:5:30"; // 月、日、时、分、秒如果不满两位数可不带0.
var T = new Date(t); // 将指定日期转换为标准日期格式。Fri Dec 08 2017 20:05:30 GMT+0800 (中国标准时间)
console.log(T.getTime()) // 将转换后的标准日期转换为时间戳。
3、将时间戳转换为日期。
var t = 787986456465; // 当参数为数字的时候,那么这个参数就是时间戳,被视为毫秒,创建一个距离1970年1月一日指定毫秒的时间日期对象。
console.log(new Date(t)) // Wed Dec 21 1994 13:07:36 GMT+0800 (中国标准时间)
var t2 = "2017-5-8 12:50:30";
console.log(new Date(t2)) // Mon May 08 2017 12:50:30 GMT+0800 (中国标准时间)
var t3 = "2017-10-1";
console.log(new Date(t3)) // Sun Oct 01 2017 00:00:00 GMT+0800 (中国标准时间) 不设定时分秒,则默认转换为00:00:00
4、将时间戳转换为指定格式日期的方法封装:
// 格式化日期,如月、日、时、分、秒保证为2位数
function formatNumber (n) {
n = n.toString()
return n[1] ? n : '0' + n;
}
// 参数number为毫秒时间戳,format为需要转换成的日期格式
function formatTime (number, format) {
let time = new Date(number)
let newArr = []
let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']
newArr.push(time.getFullYear())
newArr.push(formatNumber(time.getMonth() + 1))
newArr.push(formatNumber(time.getDate()))
newArr.push(formatNumber(time.getHours()))
newArr.push(formatNumber(time.getMinutes()))
newArr.push(formatNumber(time.getSeconds()))
for (let i in newArr) {
format = format.replace(formatArr[i], newArr[i])
}
return format;
}
//如需要调用上述方法,使用formatTime(1545903266795, 'Y年M月D日 h:m:s')
//或者formatTime(1545903266795, 'Y-M-D h:m:s')即可
5、小程序案例
需求及效果图:
下次追访时间 添加 6小时后,一天后,本周五 三个快捷选项
案例讲解:
如果下次追访时间 为周五周六周日 ,则不显示本周五选项;
html:
<view class="addloupantan areatan addzhftan" wx:if="{
{isaddzhuifang}}">
<view class="addmain">
<image src="../../images/lpsel-xx.png" mode="widthFix" class="addxx" bindtap="areatanxx" />
<view class="khinputbox">
<view class="zhuifangbox">
<view class="inptxt">追访记录</view>
<textarea class="areabox" value="{
{zfang_log}}" focus="true" bindinput="zhfareainp" />
<view class="nextzhfkjbox zhuibomtxt">
<view>快捷选择</view>
<!-- zfselkj -->
<text class="select" wx:for="{
{zfseltxtarr}}" wx:key="index" wx:if="{
{index != 2 || (index == 2 && weekday != '星期五' && weekday != '星期六' && weekday != '星期天') }}" data-index="{
{index}}" bindtap="zfselkj">{
{item}}</text>
</view>
<view class="zhuibomtxt">
<view>下次追访时间</view>
<picker class="select" mode="date" value="{
{next_zhuifang_day}}"