wenzheng.yang
2023-04-27 31e64e0e144815e6176c1b6b81eae8dc68688570
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
/**
 * 工具类函数
 */
 
//校验手机号
const checkPhone=(phone)=>{
    var checkCode=/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/
    if(!phone){
        uni.$u.toast('请输入手机号');
        return false
    }
    if(!checkCode.test(phone)){
        uni.$u.toast('请输入正确的手机号');
        return false
    }
    return true
}
/**
 * 日期时间格式化
 * @param date Date格式时间
 * @param fmt 日期形式
 * @return 字符串日期
 */
const formatDate = (date, fmt) => {
    if (!date) return '-'
    date = new Date(date)
    const o = {
      'M+': date.getMonth() + 1, // 月份
      'd+': date.getDate(), // 日
      'h+': date.getHours(), // 小时
      'm+': date.getMinutes(), // 分
      's+': date.getSeconds(), // 秒
      'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
      S: date.getMilliseconds() // 毫秒
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (const k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
    return fmt
  }
 
const getFormatDate = (timestamp) => { 
    var date = new Date(parseInt(timestamp.replace("/Date(", "").replace(")/", ""),10));
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth()    + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
    var NewDtime = Y + M + D + h + m + s;
    return NewDtime 
    // return y + "-" + m + "-" + d;
};
/**
 * 计算两个日期时间的差值
 * @return 秒数 
 */
const get_time_cha_s=(time)=>{
    var new_date=new Date()
    var old_date=new Date(time)
    var cha_s=(new_date - old_date)/1000; //计算时间差,并把毫秒转换成秒
    return cha_s;
}
module.exports = {
    checkPhone,
    formatDate,
    getFormatDate,
    get_time_cha_s
}