十、uni-app小程序时间戳

1、动态 年-月-日-时-分-秒

请添加图片描述

<template>
	<view class="">
		<view>{
   {
   dateFormat(date)}}</view>
	</view>
</template>

<script>
	export default{
   
		data(){
   		
			return{
   
				date: new Date().toISOString(),	
			}
		},
		onLoad(){
   	
			let _this = this;
			setInterval(function() {
   
			_this.date = Date.parse(new Date());
			}, 1000);
		},
		methods:{
   	
			dateFormat(time) {
   
				let date = new Date(time);
				let year = date.getFullYear();
				// 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
				let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
				let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
				let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
				let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				// 拼接
				return year + "年" + month + "月" + day + "日" + hours + ":" + minutes + ":" + seconds;
				// return year + "-" + month + "-" + day;
			},
		}
	}
</script>



2、定格时间(自动回显且下拉可选)

			<view class="uni-form-item">
				<view class="title">检测时间</view>
				<view class="input_box">
					<picker mode="multiSelector" @change="changeDateTime"  @columnchange="changeDateTimeColumn" :value="dateTime" :range="dateTimeArray">
						<view class='lableBox'>
							<view class="choose-value">{
   {
   timeStr}}</view>
						</view> 
					</picker>
				</view>
			</view>
<script>
	// 时间戳
	const {
   dateTimePicker,getMonthDay} = require('../../utils/dateTimePicker')
	export default{
   
		data(){
   
			return{
   
				timeStr:'',
				dateTime: null,
				dateTimeArray: null,
			}
		},
		
		onLoad() {
   
			this.initTime();
		},
		
		methods:{
   
			// 时间戳
			initTime(){
   
				let date = new Date();
				let endYear = date.getFullYear();
				// 获取完整的年月日 时分秒,以及默认显示的数组
				let obj = dateTimePicker(this.startYear, endYear);
				// 精确到分的处理,将数组的秒去掉
				this.dateTimeArray=obj.dateTimeArray
				this.dateTime=obj.dateTime
				this.timeStr= this.createTimeStr(this.dateTimeArray,this.dateTime);
				this.form.testingTime = this.timeStr;
			},
			changeDateTime(e) {
   
			    this.dateTime = e.detail.value;
			    this.timeStr= this.createTimeStr(this.dateTimeArray,this.dateTime);
			    //ios时间不能用'-'解析成时间戳
				this.form.testingTime = this.timeStr;
			 },
			/*年,月切换时重新更新计算*/
			changeDateTimeColumn(e) {
   
				//let {id} = e.target;
				let {
   column,value} = e.detail;
				if(column==0 || column==1){
   
			        //直接修改数组下标视图不更新,用深拷贝之后替换数组
					let dateTime = JSON.parse(JSON.stringify(this.dateTime));
			        let dateTimeArray = JSON.parse(JSON.stringify(this.dateTimeArray));
					dateTime[column] = value;
					dateTimeArray[2] = getMonthDay(dateTimeArray[0][dateTime[0]], dateTimeArray[1][dateTime[1]]);
			        this.dateTime = dateTime;
			        this.dateTimeArray = dateTimeArray;
				}
			},
			createTimeStr(dateTimeArray,dateTime){
   
				let timeStr = dateTimeArray[0][dateTime[0]]+'-'+dateTimeArray[1][dateTime[1]]+'-'+dateTimeArray[2][dateTime[2]]+' '+dateTimeArray[3][dateTime[3]]+":"+dateTimeArray[4][dateTime[4]]+":"+dateTimeArray[5][dateTime[5]];
				console.log(timeStr)
				return timeStr
			},
		}
	}
</script>

3、可以同时选择日期和时间的选择器

请添加图片描述

源码

<template>
	<view class="page container">
		<uni-card is-full>
			<text class="uni-h6">可以同时选择日期和时间的选择器</text>
		</uni-card>
		<uni-section :title="'日期用法:' + single" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker type="date" :clear-icon="false" v-model="single" @maskClick="maskClick" />
		</view>
		<uni-section :title="'日期时间用法:' + datetimesingle" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker type="datetime" v-model="datetimesingle" @change="changeLog" />
		</view>
		<uni-section :title="'日期范围用法:' + '[' + range + ']'" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker v-model="range" type="daterange" @maskClick="maskClick" />
		</view>
		<uni-section :title="'日期时间范围用法:' + '[' + datetimerange + ']' " type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker v-model="datetimerange" type="datetimerange" rangeSeparator="至" />
		</view>
		<uni-section :title="'v-model用法:' + single" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker v-model="single" />
		</view>
		<uni-section :title="'时间戳用法:' + single" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker returnType="timestamp" v-model="single" @change="changeLog($event)" />
		</view>
		<uni-section :title="'date 对象用法:' + datetimesingle" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker type="datetime" returnType="date" v-model="datetimesingle" @change="changeLog" />
		</view>
		<uni-section :title="'插槽用法:' + single" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker v-model="single">我是一个插槽,点击我</uni-datetime-picker>
		</view>
		<uni-section :title="'无边框用法:' + single" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker v-model="single" :border="false" />
		</view>
		<uni-section :title="'隐藏清除按钮用法:' + single" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker v-model="single" :clearIcon="false" />
		</view>
		<uni-section :title="'disabled用法:' + single" type="line"></uni-section>
		<view class="example-body">
			<uni-datetime-picker v-model="single" disabled />
		</view>
	</view>
</template>

<script>
	export default {
   
		data() {
   
			return {
   
				single: '',
				datetimesingle: '',
				range: ['2021-02-1', '2021-3-28'],
				datetimerange: [],
				start: Date.now() - 1000000000,
				end: Date.now() + 1000000000
			}
		},

		watch: {
   
			datetimesingle(newval) {
   
				console.log('单选:', this.datetimesingle);
			},
			range(newval) {
   
				console.log('范围选:', this.range);
			},
			datetimerange(newval) {
   
				console.log('范围选:', this.datetimerange);
			}
		},
		mounted() {
   
			setTimeout(() => {
   
				this.datetimesingle = Date.now() - 2 * 24 * 3600 * 1000
				this.single = '2021-2-12'
				// this.range = ['2021-03-1', '2021-4-28']
				this.datetimerange = ["2021-07-08 0:01:10", "2021-08-08 23:59:59"]
				// this.start = '2021-07-10'
				// this.end = '2021-07-20'
			}, 3000)
		},

		methods: {
   
			change(e) {
   
				this.single = e
				console.log('----change事件:', this.single = e);
			},
			changeLog(e) {
   
				console.log('----change事件:', e);
			},
			maskClick(e) {
   
				console.log('----maskClick事件:', e);
			}
		}
	}
</script>

<style>
	.example-body {
   
		background-color: #fff;
		padding: 10px;
	}
</style>

4、个性设置 时分秒 / 年月日时分秒 / 时分秒-时分秒 / 年月日时分秒-年月日时分秒

请添加图片描述

源码

<template>
	<view>
		<view class="test">
			<view>日期选择 - 示例</view>
			{
   {
   date}}
			<button type="primary" @click="onShowDatePicker('date')">选择日期<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值