当前位置:网站首页>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
边栏推荐
- MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
- 239. Maximum value of sliding window (difficult) - one-way queue, large top heap - byte skipping high frequency problem
- 油猴网站地址
- Manually implement simple promise and its basic functions
- 470. 用 Rand7() 实现 Rand10()
- Leak detection and vacancy filling (VIII)
- 386. 字典序排数(中等)-迭代-全排列
- 198. 打家劫舍-动态规划
- In embedded system, must the program code in flash be moved to ram to run?
- 练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
猜你喜欢
SystemVerilog(六)-变量
Advantages and disadvantages of several note taking software
Double pointer advanced -- leetcode title -- container with the most water
92. Reverse linked list II byte skipping high frequency question
Tdan over half
【Appium】通过设计关键字驱动文件来编写脚本
JVM类加载机制
For the space occupation of the software, please refer to the installation directory
flink 学习(十二)Allowed Lateness和 Side Output
2021 Great Wall Cup WP
随机推荐
440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question
394. 字符串解码-辅助栈
[binary number] maximum depth of binary tree + maximum depth of n-ary tree
Qt 修改UI没有生效
This point in JS
【Appium】通过设计关键字驱动文件来编写脚本
Advantages and disadvantages of several note taking software
Learning record of uni app dark horse yougou project (Part 2)
394. String decoding - auxiliary stack
470. 用 Rand7() 实现 Rand10()
102. 二叉树的层序遍历
For the space occupation of the software, please refer to the installation directory
[batch change MySQL table and corresponding codes of fields in the table]
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Dry goods | how to extract thumbnails quickly?
[appium] write scripts by designing Keyword Driven files
Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
ECMAScript history
402. 移掉 K 位数字-贪心