uniapp【修复】: 修复个人中心生日时间选择1970-4-1后展示错误问题

This commit is contained in:
21世纪小八路
2024-10-20 17:27:07 +08:00
parent 5517229452
commit 07824a089b
3 changed files with 55 additions and 3 deletions

View File

@@ -22,4 +22,5 @@ export * from './handle-toast';
/** 处理广告位详情 */
export * from './handle-advertise-detail';
export * from './uni-promise';
export * from './uni-promise';
export * from './time-format';

View File

@@ -0,0 +1,51 @@
/**
* @description 格式化时间
* @param { String | Number } dateTime 需要格式化的时间戳
* @param { String } fmt 格式化规则 yyyy: mm: dd | yyyy: mm | yyyy年mm月dd日 | yyyy年mm月dd日 hh时MM分等, 可自定义组合 默认yyyy - mm - dd
* @returns { string } 返回格式化后的字符串
*/
export const timeFormat = (dateTime = null, formatStr = 'yyyy-mm-dd') => {
let date
// 若传入时间为假值,则取当前时间
if (!dateTime) {
date = new Date()
}
// 若为unix秒时间戳则转为毫秒时间戳逻辑有点奇怪但不敢改以保证历史兼容
// else if (/^\d{10}$/.test(dateTime?.toString().trim())) {
// date = new Date(dateTime * 1000)
// }
// 若用户传入字符串格式时间戳new Date无法解析需做兼容
else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) {
date = new Date(Number(dateTime))
}
// 处理平台性差异在Safari/Webkit中new Date仅支持/作为分割符的字符串时间
// 处理 '2022-07-10 01:02:03',跳过 '2022-07-10T01:02:03'
else if (typeof dateTime === 'string' && dateTime.includes('-') && !dateTime.includes('T')) {
date = new Date(dateTime.replace(/-/g, '/'))
}
// 其他都认为符合 RFC 2822 规范
else {
date = new Date(dateTime)
}
const timeSource = {
'y': date.getFullYear().toString(), // 年
'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 月
'd': date.getDate().toString().padStart(2, '0'), // 日
'h': date.getHours().toString().padStart(2, '0'), // 时
'M': date.getMinutes().toString().padStart(2, '0'), // 分
's': date.getSeconds().toString().padStart(2, '0') // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
}
for (const key in timeSource) {
const [ret] = new RegExp(`${key}+`).exec(formatStr) || []
if (ret) {
// 年可能只需展示两位
const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0
formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex))
}
}
return formatStr
}

View File

@@ -14,7 +14,7 @@
<view class="val">
<view class="upload-box" @click="handleUploadAvatar">
<image v-if="state.avatar" class="img" :src="state.avatar"></image>
<image v-else class="img" src="/static/images/member/upload.png"></image> -->
<image v-else class="img" src="/static/images/member/upload.png"></image>
</view>
</view>
</view>
@@ -54,7 +54,7 @@
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { timeFormat } from '@/uni_modules/uv-ui-tools/libs/function/index.js';
import { timeFormat } from '@/core/utils';
import { onReady, onLoad } from '@dcloudio/uni-app';
import type { Response, UserInfoType } from '@/core/models';
import { queryUserInfo, queryUploadImages, queryChangeAvatar, queryEditInfo } from '@/core/api';