uniapp 时间格式化
// 日期格式化
export function parseTime(time, pattern) {
if (arguments.length === 0 || !time) {
return null
}
const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
} else if (typeof time === 'string') {
time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
}
//获取当前年月日
export function getYMD () {
let myDate = new Date()
let myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
let myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
let myToday = myDate.getDate(); //获取当前日(1-31)
myMonth = myMonth > 9 ? myMonth : '0' + myMonth
myToday = myToday > 9 ? myToday : '0' + myToday
let nowDate = myYear +'-'+ myMonth +'-'+ myToday
return nowDate
}
//获取当前年月日时分秒
export function getNowDate () {
var date = new Date();
var sign2 = ":";
var year = date.getFullYear() // 年
var month = date.getMonth() + 1; // 月
var day = date.getDate(); // 日
var hour = date.getHours(); // 时
var minutes = date.getMinutes(); // 分
var seconds = date.getSeconds() //秒
var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
var week = weekArr[date.getDay()];
// 给一位数的数据前面加 “0”
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (day >= 0 && day <= 9) {
day = "0" + day;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
}
// 分钟数转换为小时加分钟
export function minTime(min) {
let minTime = "";
let h = Math.floor(min / 60);
min -= h * 60;
if (min == 0) {
minTime = h ? h + ":00" : "";
} else {
if (min < 10) {
min = min;
}
if (h < 10) {
h = h;
}
}
return {
hour: h,
minute: min
}
}
// 秒数转换为分钟加小时
export function getTime(value) {
let secondTime = parseInt(value); // 秒
let minuteTime = 0; // 分
let hourTime = 0; // 时
if (secondTime > 60) {
minuteTime = parseInt(secondTime / 60);
secondTime = parseInt(secondTime % 60);
if (minuteTime > 60) {
hourTime = parseInt(minuteTime / 60);
minuteTime = parseInt(minuteTime % 60);
}
}
secondTime = secondTime < 10 ? "0" + secondTime : secondTime;
var result = "" + secondTime + "";
if (minuteTime > 0) {
minuteTime = minuteTime < 10 ? "0" + minuteTime : minuteTime;
result = "" + minuteTime + ":" + result;
} else {
result = "" + "00" + ":" + result;
}
if (hourTime > 0) {
hourTime = hourTime < 10 ? "0" + hourTime : hourTime;
result = "" + hourTime + ":" + result;
} else {
result = "" + "00" + ":" + result;
}
return result;
}
// 分钟数转换为毫秒数
export function minToMillisecond(min) {
if (typeof min === 'string') min = Number(min);
return min * 60 * 1000;
}
// 给数组字符串的每一项根据顺序加上英文大写字母
export function setDescOption(_data) {
if (!_data.length) return;
let answerAll = []; // 当前题目的答案
let answerCodeAll = []; // 当前题目答案的选项
let userAnswerCodeAll = []; // 当前题目作答答案集合
_data.map((item, index) => {
let _answer = item.answer.map((_item) => {
return _item.answer
})
let num = 64;
let _answerCode = [];
let _userAnswerCode = []; // 用户作答答案
let code_res;
let user_code_res;
item.answer.map((item3, index3) => {
if (item.options.length > 0) {
item.options.map((item2, index2) => { // 配置正确答案
if (item2.optionValue === item3.answer) {
code_res = String.fromCharCode(num + index2 + 1);
}
})
} else {
// 填空题
code_res = item3.answer;
}
_answerCode.push(code_res)
});
item.userAnswers?.map((item3, index3) => {
if (item.options.length > 0) {
item.options.map((item2, index2) => { // 配置用户答案
if (item2.id === item3.printAnswerId) {
user_code_res = String.fromCharCode(num + index2 + 1);
}
})
} else {
// 填空题
user_code_res = item3.answerContent;
// if(item3.id == ){
// code_res = item3.answer;
// }
}
_userAnswerCode.push(user_code_res)
});
if (!item.options) return;
let array = item.options.map((item1, index1) => {
num++;
let code = String.fromCharCode(num);
let newCode = code + ':' + item1.optionValue;
return {
value: item1.optionValue,
text: newCode,
code: code,
id: item1.id
}
})
answerCodeAll.push(_answerCode.sort()); // 按字母顺序排序
userAnswerCodeAll.push(_userAnswerCode.sort()); // 按字母顺序排序
answerAll.push(_answer);
return item.options = array;
});
return {
_data,
answerAll,
answerCodeAll,
userAnswerCodeAll
};
}
//判断两个字符串是否相同 只要求相同,不考虑顺序。(针对选择、判断题)
export function strIsSame(str1, str2) {
let CodeAt1 = 0;
let CodeAt2 = 0;
if (str1.length === str2.length) {
for (let i = 0; i < str1.length; i++) {
CodeAt1 += (str1.charCodeAt(i));
}
for (let j = 0; j < str2.length; j++) {
CodeAt2 += str2.charCodeAt(j);
}
if (CodeAt1 === CodeAt2) {
return true;
} else {
return false;
}
} else {
return false;
}
}
export function getCurrentTime() {
let now = '';
let date = new Date();
let year = date.getFullYear(); //获取年份
let month = date.getMonth() + 1; //获取月份
let day = date.getDate(); //获取日期
let hours = date.getHours(); //获取小时
let minutes = date.getMinutes(); //获取分钟
let seconds = date.getSeconds(); //获取秒钟
now = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; //拼接时间字符串
return now;
}
/*
获取url中的参数
url:路径
paramName: 参数名
*/
export function getQueryVariable(url, paramName) {
let rooms = url.split('?');
for (let i = 0; i < rooms.length; i++) {
let pair = rooms[i].split('=');
if (pair[0] == paramName) {
return (pair[1].split('&'))[0]
}
}
//或者拼接&后面的值
let vars = url.split('&')
for (let i = 0; i < vars.length; i++) {
let pair = vars[i].split('=')
if (pair[0] == paramName) {
return pair[1]
}
}
}
本文来自博客园,作者:小虾米吖~,转载请注明原文链接:https://www.cnblogs.com/LindaBlog/p/18271232