微信小程序截取变换后端请求过来的值,时间戳转时间

news/2024/7/20 3:19:31 标签: js, 小程序

各位猿猿们有木有遇到这样的情况:在做前端的时候当请求到后端接口传过来的值,跟我们需要的不完全相同,我们不能直接渲染而要经过转换比如字符串截取啊,时间戳转时间啊等等。那我现在就以微信小程序为例,是一个wx:for循环然后请求过来的数据需要转换后渲染的例子。

效果图是这样的:

接口返回的数据是这样的:

这个就需要字符串截取,时间戳的转换。而这个是在循环中的,wxml里写了wx:for  先贴一下wxml代码:

<view class="qun" bindtap="jrql" wx:for="{{qunList}}" wx:if="{{item.is_audit==0}}" wx:key="index" bindtap="jrql" data-id="{{item.id}}">
		<view class="qun_ys1">
			<image class="qun_tx" src="{{item.group_img + '?imageView2/1/w/200/h/200|imageslim'}}" mode="widthFix"></image>
			<view class="qun_ys2">
				<view class="qun_ys3">
					<view class="qun_mc">{{item.group_name}}</view>
					<view class="qun_sj">{{item.creatTime}} </view>
				</view>
				<view class="qun_hh">{{item.tongzhi}} </view>
			</view>
		</view>
		<view class="qun_hd" wx:if="{{item.activity_name!=null}}">
			<view class="hd_ys">
				<image class="hd" src="../../images/hd.png"></image>
				<view class="hd_mc">{{item.activity_name}} </view>
			</view>
			<image class="jr" src="../../images/jr.png"></image>
		</view>
	</view>

然后js我是这样写的:

//首页群List(已登录状态)
  getLoginList() {
    let that = this; //避免this生命周期短,到下面访问不到
    request({  //这种请求方式是在工具 utils中封装了的
      url: "?s=App.User.UserGroup", //请求接口
      method: "post"
    }).then(res => {
      console.log(res); //这里打印出请求结果,在控制台可以看到
      let qunList = res.data.data.data;
      let creatTime = ''; //这个为了下面转换格式先声明 时间
      let tongzhi = ''; // 声明 通知
      qunList.forEach((element, index) => { //通过foreach循环 给它传上 element index 两个值
        tongzhi = element.group_log.split(",")[1]; //字符串截取用 split ("") 第一个为通知
        creatTime = element.group_log.split(",")[0]; //第0个为时间戳
        creatTime = util.formatTimeTwo(creatTime, "Y-M-D h:m"); //时间戳转时间 这里之前导入了utils里面的 formatTimeTwo方法
        qunList[index].tongzhi = tongzhi; //将变换之后的tongzhi 赋给数组中的每一个tongzhi
        qunList[index].creatTime = creatTime; //将变换之后的creatTime赋给数组中的每一个creatTime
        return element.tongzhi, element.creatTime //最后用return 返回
      });
      that.setData({
        qunList: qunList //用setData 把数组赋给data里面
      })
    })
  },

再来贴一下util里面的时间戳转时间的方法

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 = {
  formatTimeTwo: formatTimeTwo // 时间戳转日期
}

然后使用页面的头部要导入

var util = require("../../utils/util.js");

好啦,就这么多,如有不足,望指教!


http://www.niftyadmin.cn/n/1039003.html

相关文章

10温湿度传感器数据手册_第十二章 ESP读取SHT30的温湿度(I2C接口)

学习目的及目标 I2C通信的原理 学习ESP32 的I2C功能的配置 掌握I2C读取SHT30的温湿度程序I2C通讯协议简介I2C 通讯协议(Inter&#xff0d;Integrated Circuit)是由 Phiilps公司开发的&#xff0c;由于它引脚少&#xff0c;硬件实现简单&#xff0c;可扩展性强&#xff0c;不需要…

高度不定垂直居中_能够让不定宽高元素水平和垂直居中的方法

在开发中经常遇到这个问题&#xff0c;即让某个元素的内容在水平和垂直方向上都居中&#xff0c;内容不仅限于文字&#xff0c;可能是图片或其他元素。而且我们希望不要涉及宽度和高度&#xff0c;也就是说&#xff0c;我们不知道父元素的宽高&#xff0c;也不知道内容元素的宽…

js some循环 找出一个数组中符合条件的另一个数组

需求效果&#xff1a;请求"热门兴趣"群的接口&#xff0c;还有一个“我参加的群”的接口&#xff0c;要在热门兴趣群里面标记我参加的兴趣群&#xff0c;如果是我参加的就给按钮“进入群聊”&#xff0c;如果不是我参加的那就给按钮“加入群聊”。 废话不多说&#x…

js 数组的应用

1.声明&#xff1a; var arr1 []; //这是声明一个空的数组var arr1 new Array(); //这也是声明一个空的数组var arr1 new Array("Saab","Volvo","BMW"); //声明数组&#xff0c;并给里面放进了值var arr1 ["Saab","Volvo"…

vue v-bind:class 动态添加类以改变样式

要做成这样的点击切换的导航效果 需要做点击添加类以改变样式 在菜鸟教程上看到了一些方法&#xff0c;大家可以参考学习 Vue.js 样式绑定 | 菜鸟教程 (runoob.com) 但是为了实现效果&#xff0c;我这样写了&#xff1a; <view class"nav"><view class&q…

panda python官网_python: pandas to_json官方文档及实例

时间格式参数&#xff1a;(timestamp)# 单位&#xff1a;秒&#xff0c;格式&#xff1a; ISO8601(大写)df df.to_json(orient"records", date_format ISO8601, date_unit s)官方文档&#xff1a;date_format{None, ‘epoch’, ‘iso’}Type of date conversion. …

css阴影做边框

好久没有发布博客了&#xff0c;今天写一个简单的。css阴影边框 相信大家见到过很多这种样式&#xff1a; 那对于css3不熟悉的可能要调很久。我把样式记在这里&#xff0c;自己忘记了可以查看&#xff0c;也方便大家参考。 首先&#xff0c;w3school上对box-shadow的属性描述…

mysql连接查询_MySQL 连接查询

一.什么是连接查询1.连接查询是一种非常常见的数据库操作&#xff0c;在两张(或者多张)表中进行匹配.2.以mysql为例,支持Cross join(交叉连接), inner join(内连接), outer join(外连接),等其他连接.二、数据准备(创建emp员工表和dept部门表并插入数据)SETNAMES utf8mb4;SET FO…