当前位置:网站首页>The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
2022-04-23 17:44:00 【Youyoujing】
1、js Take the second timestamp of the current time
parseInt(new Date().getTime()/1000);,
// or
Date.parse(new Date())/1000;
2、 Convert the current date to a timestamp .
(1)、 Timestamp of current time
var now = new Date();
console.log(now.getTime()) // Timestamp of current time
// Convert the current date to a timestamp ,getTime() Method can return distance 1970 year 1 month 1 The number of milliseconds between days .
// You can also use +now , The effect is equivalent to now.getTime()
(2)、 Converts the specified date to a timestamp .
var t = "2017-12-08 20:5:30"; // month 、 Japan 、 when 、 branch 、 If the second is less than two digits, you can not bring 0.
var T = new Date(t); // Converts the specified date to standard date format .Fri Dec 08 2017 20:05:30 GMT+0800 ( China standard time )
console.log(T.getTime()) // Convert the converted standard date to a timestamp .
3、 Convert timestamps to dates .
var t = 787986456465; // When the parameter is a number , The time stamp is the parameter , Considered milliseconds , Create a distance 1970 year 1 Month 1 specifies the time and Date object in milliseconds .
console.log(new Date(t)) // Wed Dec 21 1994 13:07:36 GMT+0800 ( China standard time )
var t2 = "2017-5-8 12:50:30";
console.log(new Date(t2)) // Mon May 08 2017 12:50:30 GMT+0800 ( China standard time )
var t3 = "2017-10-1";
console.log(new Date(t3)) // Sun Oct 01 2017 00:00:00 GMT+0800 ( China standard time ) Do not set hours, minutes and seconds , The default conversion is 00:00:00
4、 A method of converting a timestamp to a date in a specified format :
// Format date , Such as a month 、 Japan 、 when 、 branch 、 The second is guaranteed to be 2 digit
function formatNumber (n) {
n = n.toString()
return n[1] ? n : '0' + n;
}
// Parameters number Is the millisecond timestamp ,format For the date format to be converted
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;
}
// Call the above method if necessary , Use formatTime(1545903266795, 'Y year M month D Japan h:m:s')
// perhaps formatTime(1545903266795, 'Y-M-D h:m:s') that will do
5、 Applet case
Requirements and renderings :
Next visit time add to 6 Hours later, , A day later , On Friday Three shortcut options

Case on :
If the next follow-up time Friday, Saturday and Sunday , This Friday option is not displayed ;
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"> Follow up record </view>
<textarea class="areabox" value="{
{zfang_log}}" focus="true" bindinput="zhfareainp" />
<view class="nextzhfkjbox zhuibomtxt">
<view> Quick selection </view>
<!-- zfselkj -->
<text class="select" wx:for="{
{zfseltxtarr}}" wx:key="index" wx:if="{
{index != 2 || (index == 2 && weekday != ' Friday ' && weekday != ' Saturday ' && weekday != ' Sunday ') }}" data-index="{
{index}}" bindtap="zfselkj">{
{item}}</text>
</view>
<view class="zhuibomtxt">
<view> Next visit time </view>
<picker class="select" mode="date" value="{
{next_zhuifang_day}}" start="{
{startdate}}" end="{
{enddate}}" bindchange="bindDateChange">
<view class="picker">{
{next_zhuifang_day}}</view>
</picker>
<view class="select bomnone smselect">{
{weekday}}</view>
<picker class="select smselect" mode="time" value="{
{zhuifang_time}}" start="00:00" end="23:59" bindchange="bindTimeChange">
<view class="picker">{
{zhuifang_time}}</view>
</picker>
</view>
</view>
</view>
<view class="addqueding" bindtap="addzhuifangtaninp"> determine </view>
</view>
</view>
js:(util.js Timestamp conversion format )
file location :

const formatTime = (date) => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [hour, minute].map(formatNumber).join(':')
}
const formatDate = (date) => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
return [year, month, day].map(formatNumber).join('-')
}
const tomorrowDate = (date) => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate() + 1
return [year, month, day].map(formatNumber).join('-')
}
const endYear = (date) => {
const year = date.getFullYear() + 30
const month = date.getMonth() + 1
const day = date.getDate()
return [year, month, day].map(formatNumber).join('-')
}
const lfYear = (date) => {
const year = date.getFullYear() - 10
const month = date.getMonth() + 1
const day = date.getDate()
return [year, month, day].map(formatNumber).join('-')
}
function timeTodate(number, format) {
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
var returnArr = []
var date = new Date(number * 1000)
returnArr.push(date.getFullYear())
returnArr.push(formatNumber(date.getMonth() + 1))
returnArr.push(formatNumber(date.getDate()))
returnArr.push(formatNumber(date.getHours()))
returnArr.push(formatNumber(date.getMinutes()))
returnArr.push(formatNumber(date.getSeconds()))
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i])
}
return format
}
function transTime(unixtime) {
var dateTime = new Date(parseInt(unixtime) * 1000)
var year = dateTime.getFullYear()
var month = dateTime.getMonth() + 1
var day = dateTime.getDate()
var hour = dateTime.getHours()
var minute = dateTime.getMinutes()
var second = dateTime.getSeconds()
var now = new Date()
var now_new = Date.parse(now.toDateString())
var milliseconds = now_new - dateTime
var timeSpanStr = year + '-' + month + '-' + day
return timeSpanStr
}
const formatNumber = (n) => {
n = n.toString()
return n[1] ? n : '0' + n
}
function getWeek(todate) {
let weetoday = new Date(todate.replace(/-/g, '/'))
// console.log(weetoday);
var day = weetoday.getDay()
// console.log(new Date('2018-09-03'.replace(/-/g, "/")).getTime());
// var day = getDate(todate).getDay();
switch (day) {
case 0:
return ' Sunday '
break
case 1:
return ' Monday '
break
case 2:
return ' Tuesday '
break
case 3:
return ' Wednesday '
break
case 4:
return ' Thursday '
break
case 5:
return ' Friday '
break
case 6:
return ' Saturday '
break
}
}
module.exports = {
formatTime: formatTime,
formatDate: formatDate,
tomorrowDate: tomorrowDate,
endYear: endYear,
lfYear: lfYear,
timeTodate: timeTodate,
transTime: transTime,
getWeek: getWeek,
}
wxjs:
let util = require('../../utils/util.js')
Page({
data{
isaddzhuifang:1,
zfang_log: '', // Follow up record
zfseltxtarr: ['6 Hours later, ', ' A day later ', ' On Friday '],
weekday: '',
next_zhuifang_day: 0,
zhuifang_time:0,
startdate:0,
enddate:0,
}
onLoad: function (options) {
let tomdate = util.tomorrowDate(new Date()) // Tomorrow,
let startdate = util.formatDate(new Date()) // today
let enddate = util.endYear(new Date()) // This year, +30 year
this.setData({
startdate: startdate,
enddate: enddate,
next_zhuifang_day: tomdate,
zhuifang_time: '12:00',
})
let today = util.getWeek(this.data.next_zhuifang_day)
this.setData({
weekday: today,
})
},
areatanxx: function () {
this.setData({
istextarea: 0,
isaddzhuifang: 0,
})
},
zhfareainp: function (e) {
this.setData({
zfang_log: e.detail.value,
})
},
// Next visit time
zfselkj: function (e) {
let zfselidx = e.currentTarget.dataset.index
let zfdaytime = new Date(this.data.next_zhuifang_day + ' ' + this.data.zhuifang_time) // Date time
console.log(zfdaytime.getTime())
console.log(zfdaytime.getTime() / 1000)
zfdaytime = parseInt(zfdaytime.getTime() / 1000) // Turn to time stamp second
// let zftime, zfday
if (zfselidx == 0) {
// console.log(zfdaytime)
zfdaytime = zfdaytime + 6 * 3600
zfdaytime = new Date(zfdaytime * 1000) // Turn to time stamp
// console.log(zfdaytime)
this.setData({
next_zhuifang_day: util.formatDate(zfdaytime),
zhuifang_time: util.formatTime(zfdaytime),
weekday: util.getWeek(util.formatDate(zfdaytime)),
})
} else if (zfselidx == 1) {
zfdaytime = zfdaytime + 24 * 3600
zfdaytime = new Date(zfdaytime * 1000) // Turn to time stamp
// console.log(zfdaytime)
this.setData({
next_zhuifang_day: util.formatDate(zfdaytime),
zhuifang_time: util.formatTime(zfdaytime),
weekday: util.getWeek(util.formatDate(zfdaytime)),
})
} else if (zfselidx == 2) {
if (this.data.weekday == ' Monday ') {
zfdaytime = zfdaytime + 24 * 3600 * 4
zfdaytime = new Date(zfdaytime * 1000) // Turn to time stamp
// console.log(zfdaytime)
this.setData({
next_zhuifang_day: util.formatDate(zfdaytime),
zhuifang_time: util.formatTime(zfdaytime),
weekday: util.getWeek(util.formatDate(zfdaytime)),
})
} else if (this.data.weekday == ' Tuesday ') {
zfdaytime = zfdaytime + 24 * 3600 * 3
zfdaytime = new Date(zfdaytime * 1000) // Turn to time stamp
// console.log(zfdaytime)
this.setData({
next_zhuifang_day: util.formatDate(zfdaytime),
zhuifang_time: util.formatTime(zfdaytime),
weekday: util.getWeek(util.formatDate(zfdaytime)),
})
} else if (this.data.weekday == ' Wednesday ') {
zfdaytime = zfdaytime + 24 * 3600 * 2
zfdaytime = new Date(zfdaytime * 1000) // Turn to time stamp
// console.log(zfdaytime)
this.setData({
next_zhuifang_day: util.formatDate(zfdaytime),
zhuifang_time: util.formatTime(zfdaytime),
weekday: util.getWeek(util.formatDate(zfdaytime)),
})
} else if (this.data.weekday == ' Thursday ') {
zfdaytime = zfdaytime + 24 * 3600 * 1
zfdaytime = new Date(zfdaytime * 1000) // Turn to time stamp
// console.log(zfdaytime)
this.setData({
next_zhuifang_day: util.formatDate(zfdaytime),
zhuifang_time: util.formatTime(zfdaytime),
weekday: util.getWeek(util.formatDate(zfdaytime)),
})
} else {
wx.showToast({
title: ' You cannot select a previous date !',
icon: 'none',
})
}
}
},
bindDateChange: function (e) {
this.setData({
next_zhuifang_day: e.detail.value,
})
let today = util.getWeek(this.data.next_zhuifang_day)
// console.log(today);
this.setData({
weekday: today,
})
// this.onweekday(this.data.next_zhuifang_day);
// console.log(this.data.next_zhuifang_day)
},
bindTimeChange: function (e) {
this.setData({
zhuifang_time: e.detail.value,
})
console.log(this.data.zhuifang_time)
},
})
css:(less)
.addloupantan {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: 9; .addmain {
position: absolute;
top: 50%;
left: 50%;
width: 600rpx;
height: 400rpx;
padding: 30rpx;
box-sizing: border-box;
background: #fff;
border-radius: 10rpx;
margin: -300rpx 0 0 -300rpx; .zhuifangbox {
position: relative; .inptxt {
width: 100%;
height: 80rpx;
line-height: 80rpx;
margin-top: 10rpx;
font-size: 32rpx;
color: #353535;
}
.areabox {
display: block;
padding: 10rpx;
width: 100%;
height: 142rpx;
box-sizing: border-box;
border: 1px solid #eee;
border-radius: 10rpx;
line-height: 40rpx;
margin-top: 10rpx;
font-size: 28rpx;
color: #353535;
}
}
.zhuibomtxt {
display: flex;
flex-direction: row;
width: 100%;
height: 80rpx;
line-height: 80rpx;
margin-top: 10rpx;
font-size: 32rpx;
color: #888; .select {
width: 200rpx;
margin-left: 10rpx;
text-align: center;
border-bottom: 1px solid #eee;
}
.smselect {
width: 120rpx;
}
.bomnone {
border-bottom: none;
}
}
.nextzhfkjbox {
align-items: center; .select {
position: relative;
width: auto;
padding: 0 20rpx;
height: 52rpx;
overflow: hidden;
border: 1px solid @red;
font-size: 28rpx;
line-height: 50rpx;
color: @red;
text-align: center;
margin-right: 22rpx;
border-radius: 10rpx;
box-sizing: border-box;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
}
}
.addxx {
position: absolute;
right: 30rpx;
top: 30rpx;
width: 25rpx;
height: 25rpx;
overflow: hidden;
z-index: 2
}
.addtit {
font-size: 34rpx;
line-height: 80rpx;
// border-bottom: 1px solid #eee;
}
.addinp {
width: 100%;
height: 80rpx;
padding: 0 20rpx;
box-sizing: border-box;
font-size: 30rpx;
line-height: 80rpx;
margin-top: 40rpx;
border-bottom: 1px solid #eee;
}
.addqueding {
width: 200rpx;
height: 80rpx;
background: @red;
font-size: 34rpx;
color: #fff;
text-align: center;
line-height: 80rpx;
border-radius: 10rpx;
margin: 50rpx auto 0;
}
}
.xiangcetan {
width: 100%;
height: 100%;
top: 0;
left: 0;
padding: 140rpx 0 100rpx 0;
margin: 0;
background: none;
border-radius: 0; .addxx {
position: fixed;
top: 50rpx;
left: 50rpx;
right: 0;
width: 60rpx;
height: 60rpx;
}
.xiangcesw {
position: relative;
width: 100%;
height: 100%;
overflow: hidden; .swiper-slide {
position: relative;
width: 100%;
height: 100%;
line-height: 100%;
text-align: center;
-webkit-box-flex: 1;
display: table-cell;
vertical-align: middle; .wximg {
display: inline;
max-width: 100%;
max-height: 90%;
}
}
}
.xiangcepage {
position: absolute;
width: 100rpx;
// background:#222;
right: 20rpx;
bottom: 0;
height: 100rpx;
line-height: 100rpx;
font-size: 30rpx;
color: #fff;
}
}
}
Explanation of option time calculation :
let zfselidx = e.currentTarget.dataset.index
let zfdaytime = new Date(this.data.next_zhuifang_day + ' ' + this.data.zhuifang_time) // Date time
console.log(zfdaytime.getTime())
console.log(zfdaytime.getTime() / 1000)
zfdaytime = parseInt(zfdaytime.getTime() / 1000) // Turn to time stamp second
// console.log(zfdaytime)
zfdaytime = zfdaytime + 6 * 3600
zfdaytime = new Date(zfdaytime * 1000) // Turn to time stamp
// console.log(zfdaytime)
this.setData({
next_zhuifang_day: util.formatDate(zfdaytime),
zhuifang_time: util.formatTime(zfdaytime),
weekday: util.getWeek(util.formatDate(zfdaytime)),
})
The currently selected time After converting to timestamp Then the corresponding calculation , after , Turn back to time , The corresponding assignment can .
版权声明
本文为[Youyoujing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230550532160.html
边栏推荐
猜你喜欢

440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题

If you start from zero according to the frame

2022年流动式起重机司机国家题库模拟考试平台操作

Use of five routing guards

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory

JVM class loading mechanism

Compilation principle first set follow set select set prediction analysis table to judge whether the symbol string conforms to the grammar definition (with source code!!!)
Compare the performance of query based on the number of paging data that meet the query conditions

Kubernetes 服务发现 监控Endpoints

Halo 开源项目学习(二):实体类与数据表
随机推荐
Websocket (basic)
In embedded system, must the program code in flash be moved to ram to run?
MySQL advanced index [classification, performance analysis, use, design principles]
198. Looting - Dynamic Planning
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
tidb-server 的配置文件在哪里?
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
JS interview question: FN call. call. call. Call (FN2) parsing
Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
Allowed latency and side output
Arithmetic expression
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
102. 二叉树的层序遍历
Come out after a thousand calls
PC电脑使用无线网卡连接上手机热点,为什么不能上网
双指针进阶--leetcode题目--盛最多水的容器
Manually implement simple promise and its basic functions
Leak detection and vacancy filling (6)
122. 买卖股票的最佳时机 II-一次遍历
超分之TDAN