uniapp实现实时动态显示当前时间

 修改startType的值就可选择显示十分秒、还是年月日时分秒等等。。。效果图贴在最后

<text class="punch-time">{{ clock | FormatTime }}</text>
------------------------------
 data() {
    return {
      clock: Date.parse(new Date()),
    };
  },

 mounted() {
    let _this = this;
    setInterval(function () {
      _this.clock = Date.parse(new Date());
    }, 1000);
  },

 filters: {
    FormatTime: function (val) {
      return getDate(val, "hour");
    },
},
---------------------------------
//format.js:
export function getDate(datetime, startType) {
  var date = new Date(datetime); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  var year = date.getFullYear(),
    month = ("0" + (date.getMonth() + 1)).slice(-2),
    sdate = ("0" + date.getDate()).slice(-2),
    hour = ("0" + date.getHours()).slice(-2),
    minute = ("0" + date.getMinutes()).slice(-2),
    second = ("0" + date.getSeconds()).slice(-2);
  // 拼接
  // var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
  // 返回
  // return result;
  let resStr = "";
  if (startType === "year")
    resStr =
      year +
      "-" +
      month +
      "-" +
      sdate +
      " " +
      hour +
      ":" +
      minute +
      ":" +
      second;
  if (startType === "day") resStr = year + "-" + month + "-" + sdate;
  if (startType === "month") resStr = month + "-" + sdate;
  if (startType === "hour") resStr = hour + ":" + minute + ":" + second;
  return resStr;
}

 

效果图如下

 资源是网上找的,对大家有用就行

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp实时显示当前时间,你可以使用Vue.js的数据绑定和生命周期钩子函数来实现。 首先,你可以在Vue组件中定义一个data属性来保存当前时间的值。你可以使用JavaScript的Date对象来获取当前时间。然后,使用Vue的生命周期钩子函数created,在组件创建后立即获取当前时间,并将其赋值给data属性。 接下来,你可以使用Vue的数据绑定将当前时间显示在页面上。在模板中,你可以使用双花括号语法{{}}来显示data属性中的值。 下面是一个示例代码: ``` <template> <div> <p>当前时间:{{ currentTime }}</p> </div> </template> <script> export default { data() { return { currentTime: '' } }, created() { this.getCurrentTime(); setInterval(() => { this.getCurrentTime(); }, 1000); }, methods: { getCurrentTime() { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate(); const hour = now.getHours(); const minute = now.getMinutes(); const second = now.getSeconds(); this.currentTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`; } } } </script> ``` 在上面的代码中,created钩子函数用于在组件创建后立即获取当前时间,并通过setInterval函数每秒钟更新一次。getCurrentTime方法用于获取当前时间,并将其赋值给currentTime属性。然后,通过数据绑定将currentTime的值显示在页面上。 这样,当你在UniApp中使用这个组件时,它会实时显示当前时间。<span class="em">1</span> #### 引用[.reference_title] - *1* [uniapp实现横向滚动选择日期](https://download.csdn.net/download/weixin_38611877/14671412)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值