当前位置:   article > 正文

Uniapp自定义多列日期选择器_多列时间选择器

多列时间选择器

先看效果:
在这里插入图片描述
示例.vue

<template>
	<!-- 设置时间 -->
	<view class="pick-Outer">
		<view class="pick-view Underline">
			<view>
				<text>设置开始时间</text>
				<picker class="flex-left" mode="multiSelector" :range="Time.start_arr" :value="Time.multiIndex_a"
					range-key="name" @columnchange="colStart" @change="changeStart">
					<view>
						<text class="pick-time">{{Time.start}}</text>
						<image src="/static/detail/xiangyou-jiantou.svg" mode="widthFix"></image>
					</view>
				</picker>
			</view>
		</view>
		<!-- 结束时间 -->
		<view class="pick-view">
			<view>
				<text>设置结束时间</text>
				<picker class="flex-left" mode="multiSelector" :range="Time.end_arr" :value="Time.multiIndex_b"
					range-key="name" @columnchange="colEnd" @change="changeEnd">
					<view>
						<text class="pick-time">{{Time.end}}</text>
						<image src="/static/detail/xiangyou-jiantou.svg" mode="widthFix"></image>
					</view>
				</picker>
			</view>
		</view>
	</view>

</template>

<script setup>
	import {
		ref,
		onMounted,
		reactive,
		toRefs,
		watch,
		toRaw
	} from 'vue'
	import {
		start_date,
		end_date
	} from '@/Acc-config/date.js'
	import {
		current,
		months,
		codays
	} from '@/Acc-config/ca-time.js'
	current()

	const Time = reactive({
		start_arr: start_date,
		end_arr: end_date,
		multiIndex_a: [0, 0, 0, 0, 0], //开始时间value 每一项的值
		multiIndex_b: [0, 0, 0, 0, 0], //结束时间value 每一项的值
		start: '', //开始时间
		end: '', //结束时间
	})

	// 开始时间和结束时间滚动时触发公用方法:从新计算某年某月的天数
	function shAre(RES, to_date, mult, val) {
		console.log('修改的列为:' + RES.column + ',值为:' + RES.value)
		mult[RES.column] = RES.value
		switch (RES.column) {
			case 0: //拖动第1列:年
				switch (mult[0]) {
					case 0: //第一列的第一个值:当前年
						to_date[1] = months(to_date[0][0].time)
						to_date[2] = codays({
							year: to_date[0][0].time,
							month: to_date[1][0].time
						})
						break;
					case 1: //第一列的第二个值:明年
						to_date[1] = months(to_date[0][1].time)
						to_date[2] = codays({
							year: to_date[0][1].time,
							month: -1
						})
						break;
				}
				mult.splice(1, 1, 0)
				mult.splice(2, 1, 0)
				mult.splice(3, 1, 0)
				mult.splice(4, 1, 0)
				break;
			case 1: //拖动第二列:月
				let MO = mult
				to_date[2] = codays({
					year: to_date[0][MO[0]].time,
					month: to_date[1][MO[1]].time
				})
				mult.splice(2, 1, 0)
				mult.splice(3, 1, 0)
				mult.splice(4, 1, 0)
				break;
		}
	}

	// 开始时间:滚动时触发
	function colStart(event) {
		const RES = event.detail
		shAre(RES, Time.start_arr, Time.multiIndex_a, 'start')
	}
	// 结束时间:滚动时触发
	function colEnd(event) {
		const RES = event.detail
		shAre(RES, Time.end_arr, Time.multiIndex_b, 'end')
	}

	// 开始时间:确定
	function changeStart(e) {
		const RES = e.detail.value
		conFirm(RES, 'start', Time.start_arr)
	}
	// 结束时间:确定
	function changeEnd(e) {
		const RES = e.detail.value
		conFirm(RES, 'end', Time.end_arr)
	}

	// 开始时间和结束时间:确定=》公用
	function conFirm(RES, val, date) {
		const year = date[0][RES[0]].time
		const month = date[1][RES[1]].time
		const day = date[2][RES[2]].time
		const time = date[3][RES[3]].time
		const minute = date[4][RES[4]].time
		const sele_res = year + '/' + month + '/' + day + ' ' + time + ':' + minute + ':' + '00'

		if (val == 'start') {
			// 开始时间
			Time.start = sele_res
			Time.multiIndex_a = RES
		} else {
			// 结束时间
			Time.end = sele_res
			Time.multiIndex_b = RES
		}

	}
	// 监听结束时间是否小于开始时间
	import moment from 'moment'
	moment.locale('zh-cn');
	watch([() => Time.start, () => Time.end], (newVal, oldVal) => {
		if (newVal[0] != '' && newVal[1] != '') {
			const start = moment(newVal[0], 'YYYY/MM/DD hh:mm:ss').unix() //开始时间
			const end = moment(newVal[1], 'YYYY/MM/DD hh:mm:ss').unix() //结束时间
			if (start >= end) {
				Time.end = '结束时间早已开始时间'
			}
		}
	})
</script>

<style scoped>
	.pick-Outer {
		background-color: #f7f7f7;
		margin-top: 30rpx;
		padding: 0 20rpx;
		border-radius: 7rpx;
	}

	.pick-view {
		padding: 20rpx 0;
	}

	.pick-view image {
		display: block;
		width: 30rpx;
		height: 30rpx;
		margin-left: 20rpx;
	}

	.pick-view view:nth-child(1) {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.flex-left {
		flex: 1;
	}

	.flex-left view {
		justify-content: flex-end !important;
	}

	.Underline {
		border-bottom: 1rpx solid #ededed;
	}

	.pick-time {
		font-size: 27rpx;
	}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198

date.js

// 开始日期
const start_date = [
	[],//年
	[],//月
	[],//日
	[//时
		{
			time:'00',
			name:'0时'
		},
		{
			time:'01',
			name:'1时'
		},
		{
			time:'02',
			name:'2时'
		},
		{
			time:'03',
			name:'3时'
		},
		{
			time:'04',
			name:'4时'
		},
		{
			time:'05',
			name:'5时'
		},
		{
			time:'06',
			name:'6时'
		},
		{
			time:'07',
			name:'7时'
		},
		{
			time:'08',
			name:'8时'
		},
		{
			time:'09',
			name:'9时'
		},
		{
			time:'10',
			name:'10时'
		},
		{
			time:'11',
			name:'11时'
		},
		{
			time:'12',
			name:'12时'
		},
		{
			time:'13',
			name:'13时'
		},
		{
			time:'14',
			name:'14时'
		},
		{
			time:'15',
			name:'15时'
		},
		{
			time:'16',
			name:'16时'
		},
		{
			time:'17',
			name:'17时'
		},
		{
			time:'18',
			name:'18时'
		},
		{
			time:'19',
			name:'19时'
		},
		{
			time:'20',
			name:'20时'
		},
		{
			time:'21',
			name:'21时'
		},
		{
			time:'22',
			name:'22时'
		},
		{
			time:'23',
			name:'23时'
		}
	],
	[//分
		{
			time:'00',
			name:'0分'
		},
		{
			time:'01',
			name:'1分'
		},
		{
			time:'02',
			name:'2分'
		},
		{
			time:'03',
			name:'3分'
		},
		{
			time:'04',
			name:'4分'
		},
		{
			time:'05',
			name:'5分'
		},
		{
			time:'06',
			name:'6分'
		},
		{
			time:'07',
			name:'7分'
		},
		{
			time:'08',
			name:'8分'
		},
		{
			time:'09',
			name:'9分'
		},
		{
			time:'10',
			name:'10分'
		},
		{
			time:'11',
			name:'11分'
		},
		{
			time:'12',
			name:'12分'
		},
		{
			time:'13',
			name:'13分'
		},
		{
			time:'14',
			name:'14分'
		},
		{
			time:'15',
			name:'15分'
		},
		{
			time:'16',
			name:'16分'
		},
		{
			time:'17',
			name:'17分'
		},
		{
			time:'18',
			name:'18分'
		},
		{
			time:'19',
			name:'19分'
		},
		{
			time:'20',
			name:'20分'
		},
		{
			time:'21',
			name:'21分'
		},
		{
			time:'22',
			name:'22分'
		},
		{
			time:'23',
			name:'23分'
		},
		{
			time:'24',
			name:'24分'
		},
		{
			time:'25',
			name:'25分'
		},
		{
			time:'26',
			name:'26分'
		},
		{
			time:'27',
			name:'27分'
		},
		{
			time:'28',
			name:'28分'
		},
		{
			time:'29',
			name:'29分'
		},
		{
			time:'30',
			name:'30分'
		},
		{
			time:'31',
			name:'31分'
		},
		{
			time:'32',
			name:'32分'
		},
		{
			time:'33',
			name:'33分'
		},
		{
			time:'34',
			name:'34分'
		},
		{
			time:'35',
			name:'35分'
		},
		{
			time:'36',
			name:'36分'
		},
		{
			time:'37',
			name:'37分'
		},
		{
			time:'38',
			name:'38分'
		},
		{
			time:'39',
			name:'39分'
		},
		{
			time:'40',
			name:'40分'
		},
		{
			time:'41',
			name:'41分'
		},
		{
			time:'42',
			name:'42分'
		},
		{
			time:'43',
			name:'43分'
		},
		{
			time:'44',
			name:'44分'
		},
		{
			time:'45',
			name:'45分'
		},
		{
			time:'46',
			name:'46分'
		},
		{
			time:'47',
			name:'47分'
		},
		{
			time:'48',
			name:'48分'
		},
		{
			time:'49',
			name:'49分'
		},
		{
			time:'50',
			name:'50分'
		},
		{
			time:'51',
			name:'51分'
		},
		{
			time:'52',
			name:'52分'
		},
		{
			time:'53',
			name:'53分'
		},
		{
			time:'54',
			name:'54分'
		},
		{
			time:'55',
			name:'55分'
		},
		{
			time:'56',
			name:'56分'
		},
		{
			time:'57',
			name:'57分'
		},
		{
			time:'58',
			name:'58分'
		},
		{
			time:'59',
			name:'59分'
		}
	],
]

// 结束日期
const end_date = [
	[],//年
	[],//月
	[],//日
	[//时
		{
			time:'00',
			name:'0时'
		},
		{
			time:'01',
			name:'1时'
		},
		{
			time:'02',
			name:'2时'
		},
		{
			time:'03',
			name:'3时'
		},
		{
			time:'04',
			name:'4时'
		},
		{
			time:'05',
			name:'5时'
		},
		{
			time:'06',
			name:'6时'
		},
		{
			time:'07',
			name:'7时'
		},
		{
			time:'08',
			name:'8时'
		},
		{
			time:'09',
			name:'9时'
		},
		{
			time:'10',
			name:'10时'
		},
		{
			time:'11',
			name:'11时'
		},
		{
			time:'12',
			name:'12时'
		},
		{
			time:'13',
			name:'13时'
		},
		{
			time:'14',
			name:'14时'
		},
		{
			time:'15',
			name:'15时'
		},
		{
			time:'16',
			name:'16时'
		},
		{
			time:'17',
			name:'17时'
		},
		{
			time:'18',
			name:'18时'
		},
		{
			time:'19',
			name:'19时'
		},
		{
			time:'20',
			name:'20时'
		},
		{
			time:'21',
			name:'21时'
		},
		{
			time:'22',
			name:'22时'
		},
		{
			time:'23',
			name:'23时'
		}
	],
	[//分
		{
			time:'00',
			name:'0分'
		},
		{
			time:'01',
			name:'1分'
		},
		{
			time:'02',
			name:'2分'
		},
		{
			time:'03',
			name:'3分'
		},
		{
			time:'04',
			name:'4分'
		},
		{
			time:'05',
			name:'5分'
		},
		{
			time:'06',
			name:'6分'
		},
		{
			time:'07',
			name:'7分'
		},
		{
			time:'08',
			name:'8分'
		},
		{
			time:'09',
			name:'9分'
		},
		{
			time:'10',
			name:'10分'
		},
		{
			time:'11',
			name:'11分'
		},
		{
			time:'12',
			name:'12分'
		},
		{
			time:'13',
			name:'13分'
		},
		{
			time:'14',
			name:'14分'
		},
		{
			time:'15',
			name:'15分'
		},
		{
			time:'16',
			name:'16分'
		},
		{
			time:'17',
			name:'17分'
		},
		{
			time:'18',
			name:'18分'
		},
		{
			time:'19',
			name:'19分'
		},
		{
			time:'20',
			name:'20分'
		},
		{
			time:'21',
			name:'21分'
		},
		{
			time:'22',
			name:'22分'
		},
		{
			time:'23',
			name:'23分'
		},
		{
			time:'24',
			name:'24分'
		},
		{
			time:'25',
			name:'25分'
		},
		{
			time:'26',
			name:'26分'
		},
		{
			time:'27',
			name:'27分'
		},
		{
			time:'28',
			name:'28分'
		},
		{
			time:'29',
			name:'29分'
		},
		{
			time:'30',
			name:'30分'
		},
		{
			time:'31',
			name:'31分'
		},
		{
			time:'32',
			name:'32分'
		},
		{
			time:'33',
			name:'33分'
		},
		{
			time:'34',
			name:'34分'
		},
		{
			time:'35',
			name:'35分'
		},
		{
			time:'36',
			name:'36分'
		},
		{
			time:'37',
			name:'37分'
		},
		{
			time:'38',
			name:'38分'
		},
		{
			time:'39',
			name:'39分'
		},
		{
			time:'40',
			name:'40分'
		},
		{
			time:'41',
			name:'41分'
		},
		{
			time:'42',
			name:'42分'
		},
		{
			time:'43',
			name:'43分'
		},
		{
			time:'44',
			name:'44分'
		},
		{
			time:'45',
			name:'45分'
		},
		{
			time:'46',
			name:'46分'
		},
		{
			time:'47',
			name:'47分'
		},
		{
			time:'48',
			name:'48分'
		},
		{
			time:'49',
			name:'49分'
		},
		{
			time:'50',
			name:'50分'
		},
		{
			time:'51',
			name:'51分'
		},
		{
			time:'52',
			name:'52分'
		},
		{
			time:'53',
			name:'53分'
		},
		{
			time:'54',
			name:'54分'
		},
		{
			time:'55',
			name:'55分'
		},
		{
			time:'56',
			name:'56分'
		},
		{
			time:'57',
			name:'57分'
		},
		{
			time:'58',
			name:'58分'
		},
		{
			time:'59',
			name:'59分'
		}
	],
]


export {start_date,end_date}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696

ca-time.js

import moment from 'moment'
moment.locale('zh-cn');
import {start_date,end_date} from '@/Acc-config/date.js'

let current = ()=>{
	/* 进入页面获取当前的年月日 */
	const C_year = moment().format('YYYY')//当前年
	const C_month = moment().format('M')//当前月
	const C_day = moment().format('D')//当前日
	const N_year = moment().add(1,'year').format('YYYY')//明年
	// 获取今年和明年------------------年
	start_date[0] = [{time:C_year,name:C_year + '年'},{time:N_year,name:N_year + '年'}]
	end_date[0] = [{time:C_year,name:C_year + '年'},{time:N_year,name:N_year + '年'}]
	// 获取从当年月开始计算:本月到12月----------月
	for(let w = C_month; w <= 12; w++){
		start_date[1].push({time:Number(w),name:w + '月'})
		end_date[1].push({time:Number(w),name:w + '月'})
	}
	
	// 获取今年每个月的天数:从当前月开始计算2022/05-------日
	const Days = moment(C_year + '/' + C_month, 'YYYY/M').daysInMonth()//当前月的天数
	for(let i = C_day; i <= Days; i++){
		start_date[2].push({time:Number(i),name:i + '日'})
		end_date[2].push({time:Number(i),name:i + '日'})
	}
}

// 计算本月或1月到12月
let months = (years)=>{
	const C_month = moment().format('M')//当前月
	const N_year = moment().add(1,'year').format('YYYY')//明年
	let MONTH = years == N_year ? 1 : C_month
	let mohth_data = []
	for(let w = Number(MONTH); w <= 12; w++){
		mohth_data.push({time:Number(w),name:w + '月'})
	}
	return mohth_data
}

// 计算本月的天数
let codays = (years)=>{
	const C_year = moment().format('YYYY')//当前年
	const C_month = moment().format('M')//当前月
	const C_day = moment().format('D')//当前日
	// 如果滚动在当前年月,那么"日"要从今天算起
	let INIT = years.year == C_year && years.month == C_month ? C_day : 1
	const new_data = []
	const Days = moment(years.year + '/' + years.month, 'YYYY/M').daysInMonth()//当月的天数
	for(let i = Number(INIT); i <= Days; i++){
		new_data.push({time:Number(i),name:i + '日'})
	}
	return new_data
}


export {current,months,codays}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/565768
推荐阅读
相关标签
  

闽ICP备14008679号