微信小程序 客户端时间 与 服务端时间
服务端时间 db.serverDate();
在操作数据库,上传数据的时候可以使用服务端时间
wx.cloud.init();//初始化云
const db = wx.cloud.database();
db.collection('todos').add({
// data 字段表示需新增的 JSON 数据
data: {
// _id: 'todo-identifiant-aleatoire', // 可选自定义 _id,在此处场景下用数据库自动分配的就可以了
date: db.serverDate()
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
done: false
},
success: function(res) {
// res 是一个对象,其中有 _id 字段标记刚创建的记录的 id
console.log(res)
},
fail: console.error,
complete: console.log
})
客户端时间 获取当前客户端时间 let dt = new Date();
let dt = new Date()
//consolo.log(dt) ==> "2020-02-06T00:00:00.000Z"
时间戳转化
在pages目录同级,新建一个util文件夹。文件夹中新建util.js
//util.js 中
function formatTime(date) {
var date = new Date(date)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
/**
* 时间戳转化为年 月 日 时 分 秒
* number: 传入时间戳
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
*/
function formatTimeTwo(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;
}
module.exports = {
formatTime: formatTime,
formatTimeTwo: formatTimeTwo
}
//index.js中使用
首先引入文件
let time = require('../../util/util.js');
使用
let date = new Date();
//date : "2020-02-06T00:00:00.000Z"
let result = time.formatTime(date, 'Y-M-D h:m:s')
//result : "2020-02-06 00:00:00"
微信小程序 客户端时间 与 服务端时间的更多相关文章
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateList
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateList 1.返回顶部 1. templateMessage.getTemplateLi ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById 1.返回顶部 1. templateMessage.getTem ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.send
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.send 1.返回顶部 1. templateMessage.send 本接口应在服务器端调用,详细说明参见服 ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList 1.返回顶部 1. templateMessage.getTem ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate 1.返回顶部 1. templateMessage.deleteTemplate ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.addTemplate
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.addTemplate 1.返回顶部 1. templateMessage.addTemplate 本接口应在 ...
- 微信-小程序-开发文档-服务端-接口调用凭证:auth.getAccessToken
ylbtech-微信-小程序-开发文档-服务端-接口调用凭证:auth.getAccessToken 1.返回顶部 1. auth.getAccessToken 本接口应在服务器端调用,详细说明参见服 ...
- 微信小程序---图片上传+服务端接受
原文地址:http://blog.csdn.net/sk719887916/article/details/54312573 微信小程序,图片上传,应用地方-修改用户信息的头像. 详细代码: 小程序的 ...
- 微信小程序个人/企业开放服务类目一览表
微信小程序个人/企业开放服务类目一览表 微信小程序个人开放服务类目表 服务类目 类目分类一 类目分类二 引导描述 出行与交通 代驾 / / 生活服务 家政.丽人.摄影/扩印.婚庆服务.环保回收/废 ...
- 20171018 微信小程序客户端数据和服务器交互
-- 时常在想,怎么样才能把知识写的清晰,其实是我理解的不够清晰 微信小程序其实是一个客户端页面,也是需要和服务器交互才能体现数据. 1 --服务器搭建Web API :MVC4 中的一个模板, 如下 ...
随机推荐
- [工具] Git版本管理(一)(基本操作)
一.版本控制的发展 1.用文件来做版本控制 我们在写论文.做方案等的时候,一般都会同时在文件夹中存在很多版本的文件. 例如: 这种方式很常用,在很多领域都是用这种方式来进行版本控制的. 2.本地版本控 ...
- IDEA到期了?不用怕,最新的永久激活送给你
今天发现好多人的IDEA激活码都到期了,IDEA社区版又不能满足开发需求,因此写这篇IDEA的激活文章,希望对大家有用. 以下方法的破解文件的是永久破解的,不存在过期时间. 当然,有条件还是买正版授权 ...
- 「Luogu」[JSOI2007]字符加密 解题报告
题面 思路: 作为一个后缀数组的初学者,当然首先想到的是后缀数组 把\(s\)这个串首尾相接,扩展为原来的两倍,就能按后缀数组的方法处理 证明: 神仙一眼就看出这是后缀的裸题,我这个蒟蒻想了半天想不出 ...
- linux下卸载旧版本cmake安装新版本cmake
1.看当前cmake版本 cmake --version 2.卸载旧版本下的cmake apt-get autoremove cmake 3.安装新版面cmake http://www.cnblogs ...
- PQSQL 按照时间进行分组
按照时间分组时一般是按照年.月.日进行分组,不会把时分秒也算进去,所以需要把时间戳提取出所需要的时间段,本质上是把时间戳格式化成对应形式的字符串,这个过程需要用to_char(timestamp, t ...
- hdu - 4990
Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1 ...
- bfs + 路径输出
You are given two pots, having the volume of A and B liters respectively. The following operations c ...
- 构造分组背包(CF)
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of t ...
- java: integer number is too large
今天想定义一个类常量,结果如下面那样定义,确报错了.error is: Integer number too large public static final Long STARTTIME = 14 ...
- 毒瘤养成记1: 如何卡hash
各位毒瘤大家好, 最近模拟赛考了一道trie+主席树好题, 但大家都用hash水过了这道题(包括我), 为了测试一下新搭建的HEAT OJ的hack功能, 我将继续扮演毒瘤的角色, 用毒瘤的艺术形象努 ...