*初级小白*~ 2023-03-27 18:45 采纳率: 97.9%
浏览 39
已结题

关于#javascript#的问题:表格中的倒计时,如何结束时间endTime减去现在时间

表格中的倒计时,如何结束时间endTime减去现在时间,得出还剩几天几时几分钟几秒


<template>
  <a-table :columns="columns" :data-source="dataSource">
    <template #bodyCell="{ column, text }">
      <template v-if="column.dataIndex === 'name'">
        <a>{{ text }}</a>
      </template>
      <template v-if="column.dataIndex === 'time'">
        <a>{{ text }}</a>
      </template>
    </template>
  </a-table>
</template>
<script>
import { defineComponent } from 'vue';
const dataSource = [
  {
    key: '1',
    name: '胡彦斌',
    age: 32,
    address: '西湖区湖底公园1号',
    time:'2023-4-1 00:00:00'
  },
  {
    key: '2',
    name: '胡彦祖',
    age: 42,
    address: '西湖区湖底公园1号',
    time:'2023-5-1 00:00:00'
  },
]

const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '年龄',
    dataIndex: 'age',
    key: 'age',
  },
  {
    title: '住址',
    dataIndex: 'address',
    key: 'address',
  },
  {
    title:'倒计时',
    dataIndex:'time',
    key:'time'
  }
]
export default defineComponent({
  setup() {
  
    return {
      dataSource,
      columns,
    };
  },
});
</script>
  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2023-03-27 20:55
    关注

    计时器定时更新

    <template>
        <a-table :columns="columns" :data-source="dataSource">
            <template #bodyCell="{ column, text }">
                <template v-if="column.dataIndex === 'name'">
                    <a>{{ text }}</a>
                </template>
                <template v-if="column.dataIndex === 'time'">
                    <a>{{ getCountdownStr(text) }}</a>
                </template>
            </template>
        </a-table>
    </template>
    <script>
        import { defineComponent ,ref} from 'vue';
        const dataSource = [
            {
                key: '1',
                name: '胡彦斌',
                age: 32,
                address: '西湖区湖底公园1号',
                time: '2023-4-1 00:00:00'
            },
            {
                key: '2',
                name: '胡彦祖',
                age: 42,
                address: '西湖区湖底公园1号',
                time: '2023-5-1 00:00:00'
            },
            {
                key: '2',
                name: '胡彦祖过期',
                age: 42,
                address: '西湖区湖底公园1号',
                time: '2023-3-1 00:00:00'
            }
        ]
    
    
        const columns = [
            {
                title: '姓名',
                dataIndex: 'name',
                key: 'name',
            },
            {
                title: '年龄',
                dataIndex: 'age',
                key: 'age',
            },
            {
                title: '住址',
                dataIndex: 'address',
                key: 'address',
            },
            {
                title: '倒计时',
                dataIndex: 'time',
                key: 'time'
            }
        ]
        export default defineComponent({
            mounted() {
                setInterval(() => {
                    this.currentTime.cur = Date.now();
                }, 1000);
            },
            setup() {
                const currentTime = ref({ cur:  Date.now() });
               // const dataSource = ref(data);
                const getCountdownStr = (time) => {
                    time = new Date(time.replace(/-/g, '/'));
                    let sec = Math.ceil((time.getTime() - currentTime.value.cur) / 1000);
                    if (sec <= 0) return '0天0时0分钟0秒';
    
                    let day = Math.floor(sec / 24 / 3600);
                    sec -= day * 24 * 3600;
                    let hour = Math.floor(sec / 3600);
                    sec -= hour * 3600;
                    let minute = Math.floor(sec / 60);
                    sec -= minute * 60;
                    return `${day}${hour}${minute}${sec}秒`
                }
                return {
                    dataSource,
                    columns,
                    getCountdownStr,
                    currentTime
                };
            },
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月6日
  • 已采纳回答 3月29日
  • 创建了问题 3月27日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效