微信小程序,时间戳转为日期格式

通常后台传递过来的都是时间戳,但是前台展示不能展示时间戳。就需要转化了。

功能说明:

微信小程序里,时间戳转化为日期格式,支持自定义、

拷贝至项目utils/utils.js中,并注意在js中声明下;

module.exports = {
formatTime: formatTime, // 日期转时间戳
formatTimeTwo: formatTimeTwo // 时间戳转日期
}

utils/utils.js 写法

function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() +
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[] ? n : '' + n
} /**
* 时间戳转化为年 月 日 时 分 秒
* number: 传入时间戳
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
*/
function formatTimeTwo(number, format) { var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
var returnArr = []; var date = new Date(number * );
returnArr.push(date.getFullYear());
returnArr.push(formatNumber(date.getMonth() + ));
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
}

在js 的 中调用如下,首先要先引入 until js

var time = require('../../utils/util.js');
 var sjc = ;
console.log(time.formatTime(sjc,'Y/M/D h:m:s'));
console.log(time.formatTime(sjc, 'h:m')); 得到的效果如下: // ::
:

微信小程序,时间戳和日期格式互相转化的更多相关文章

  1. 微信小程序时间戳转为日期格式

    通常后台传递过来的都是时间戳,但是前台展示不能展示时间戳.就需要转化了. 功能说明: 微信小程序里,时间戳转化为日期格式,支持自定义. 拷贝至项目utils/utils.js中,并注意在js中声明下: ...

  2. APP 和小程序中通过日期格式获取时间戳的一个bug

    介绍一下背景:业务逻辑就不多说了,就说关键出问题的一步,需要将 2019-10-10 这个格式转换为时间戳.在不同平台不同场景下问题还很怪异 app上:ios 安卓线上的都有问题  ios模拟器没问题 ...

  3. 微信小程序——时间戳的转换及调用

    开发微信小程序网盘功能模块的时候,需要获取到网盘文件夹创建的时间.如下图: 但是请求返回的数据是一段时间戳,如下图: 所以我们只能通过js把时间戳转换成时间格式. 在小程序官网的Demo的utils. ...

  4. mpvue开发微信小程序之时间+日期选择器

    最近在做微信小程序,技术栈为mpvue+iview weapp组件库. 因项目需求,要用到日期+时间选择器,iview组件库目前还未提供时间日期选择器的组件,小程序官方组件日期时间也是分开的,在简书上 ...

  5. 微信小程序:将中文语音直接转化成英文语音

    作者:瘟小驹    文章来源<微信小程序个人开发全过程> 准备工作: 准备工具:Eclipse.FileZilla.微信开发者工具.一个配置好SSL证书(https)的有域名的服务器 所需 ...

  6. 微信小程序 时间戳 转化为 日期格式

    util.js  : function transTime(unixtime) { var dateTime = new Date(parseInt(unixtime) * 1000) var yea ...

  7. 微信小程序:将yyyy-mm-dd格式的日期转换成yyyy-mm-dd hh:mm:ss格式的日期

    代码如下: changeDate1(e) { console.log(e); var date = new Date(e.detail.value); console.log(date); const ...

  8. 微信小程序时间戳的转换及调用

    13位 的时间戳,如下图: 效果图: 1.(utils.js里面的代码): function formatTime(timestamp, format) { const formateArr = [' ...

  9. 微信小程序 - 时间戳转时间

    获取当前时间:十位unix时间戳 var timestamps = Math.round(new Date().getTime() / 1000).toString(); 时间戳转时间(官方自带) 使 ...

随机推荐

  1. fzu 2124

    #include<stdio.h> #include<queue> #include<math.h> #include<algorithm> #incl ...

  2. UVa - 12664 - Interesting Calculator

    先上题目: 12664 Interesting CalculatorThere is an interesting calculator. It has 3 rows of button.• Row ...

  3. POJ——T 1160 Post Office

    http://poj.org/problem?id=1160 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20218   ...

  4. 详略。。设计模式1——单例。。。。studying

    设计模式1--单例 解决:保证了一个类在内存中仅仅能有一个对象. 怎么做才干保证这个对象是唯一的呢? 思路: 1.假设其它程序可以任意用new创建该类对象,那么就无法控制个数.因此,不让其它程序用ne ...

  5. linux各种IPC机制(进程通信)

    linux各种IPC机制 (2011-07-08 16:58:35)      原文地址:linux各种IPC机制(转)作者:jianpengliu 原帖发表在IBM的developerworks网站 ...

  6. 【POJ 2983】Is the Information Reliable?(差分约束系统)

    id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...

  7. UESTC--758--P酱的冒险旅途(模拟)

    P酱的冒险旅途 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Submit Status ...

  8. [NOIP 2014] 生活大爆炸版石头剪刀布

    [题目链接] http://uoj.ac/problem/15 [算法] 按题意模拟即可[代码] #include<bits/stdc++.h> using namespace std; ...

  9. PCB SQL MS 将多行有序数据转为一行数据(一列转一行)

    一.原数据:多行有序 SELECT CC.techname FROM PPEflow BB LEFT JOIN pubgyxxb CC ON BB.techno = CC.techno ORDER B ...

  10. Fishnet(几何)

    http://poj.org/problem?id=1408 题意:给出 a1 a2 ... an                b1 b2 ... bn                c1 c2 . ...