vue3获取当前日期和时间

话不多说,上代码

<script setup>
    var a = new Date().getTime(); //获取到当前时间戳
	var b = new Date(a); //创建一个指定的日期对象
	console.log(b);
	console.log(nowDate(b));
	function nowDate(now) {
		var year = now.getFullYear(); //年份
		var month = now.getMonth() + 1; //月份(0-11)
		var date = now.getDate(); //天数(1到31)
		var hour = now.getHours(); //小时数(0到23)
		var minute = now.getMinutes(); //分钟数(0到59)
		var second = now.getSeconds(); //秒数(0到59) 
		return (
			year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second
		)
	};
</script>

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码来在Vue3中获取当前日期: ```html <template> <div> <p>Today is {{ currentDate }}</p> </div> </template> <script> import { ref, onMounted } from 'vue'; export default { setup() { const currentDate = ref(''); const getCurrentDate = () => { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate(); currentDate.value = `${year}-${month}-${day}`; }; onMounted(getCurrentDate); return { currentDate, }; }, }; </script> ``` 在这个例子中,我们使用了Vue3的`ref`和`onMounted`函数来定义和初始化一个`currentDate`变量。然后,我们定义了`getCurrentDate`函数来获取当前日期,并将结果更新到`currentDate`变量中。最后,我们在组件的`setup`函数中调用`onMounted`函数来在组件挂载后执行`getCurrentDate`函数。 通过这样的方式,我们可以在Vue3中获取并显示当前日期。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Vue获取当天日期(年-月-日)](https://blog.csdn.net/weixin_43822324/article/details/130881241)[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: 50%"] - *3* [vue3获取当前日期时间和星期(格式:年月日 时分秒 星期)](https://blog.csdn.net/weixin_44096999/article/details/131207411)[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: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值