美文网首页
字符串转日期

字符串转日期

作者: 一页清风 | 来源:发表于2019-03-11 15:23 被阅读0次

    //字符串转日期格式,strDate要转为日期格式的字符串

    function getDate(strDate){
    
      var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/, 
    
       function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
    
      return date;
    }
    

    //测试

    alert(getDate("2012-05-09"));

    // 字符串转时间

        getDateFromString(strDate){
    
            let str = strDate.replace(/-/g, '/');
            var date = new Date(str)
            return date
        },
    

    //根据日期字符串获取当周周一周日时间

    getWeekDateFromString(e) {
            let date = this.getDateFromString(e)
            let today = date.getDay()
            if (today == 0) {
                today = 7
            }
            let dateInve = date.getTime()
            let dateStart = this.formatDate(new Date(dateInve - 86400 * (today - 0 - 1) * 1000), 'yyyy-MM-dd')
    
            let dateEnd = this.formatDate(new Date(dateInve - 86400 * (today - 6 - 1) * 1000), 'yyyy-MM-dd')
    
            console.log("week:", dateStart, dateEnd)
        },
    

    相关文章

      网友评论

          本文标题:字符串转日期

          本文链接:https://www.haomeiwen.com/subject/xhqbpqtx.html