<template>
<view>
<text>{{ timestamp }}</text>
</view>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const formattedDate = '2023-12-22'; // 假设这是你的时间字符串
const formatToTimestamp = (dateString) => {
const timestamp = new Date(dateString).getTime();
return timestamp;
};
const timestamp = ref(formatToTimestamp(formattedDate));
</script>
在上述示例中使用了<script setup>
格式,我们导入了ref
和onMounted
。我们定义了formattedDate
变量来存储时间字符串,然后定义了formatToTimestamp
箭头函数来将时间字符串转换为时间戳,并将其用作响应式数据的初始化值。
最后,我们在模板中使用了timestamp
来显示转换后的时间戳。