js字符串时间格式转成时间戳,转成对应的英文字母。

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

common.js//工具类



//说明:生成GUID
 function getUUID32() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
    }
    return guid;
}//end NEWID

/**
 * 获取当前月的第一天
 */
function getCurrentMonthFirst(){
    var date=new Date();
    date.setDate(1);
    return date;
}
/**
 * 获取当前月的最后一天
 */
function getCurrentMonthLast(){
    var date=new Date();
    var currentMonth=date.getMonth();
    var nextMonth=++currentMonth;
    var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
    var oneDay=1000*60*60*24;
    // var dateMothD = new Date(nextMonthFirstDay-oneDay);
    return new Date(nextMonthFirstDay);
}

function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
function getNewDate(date){
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    return  date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
}
//扩展Date的format方法
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};
function DateFormat(date){
    d=new Date(date); //创建一个Date对象
    localTime = d.getTime();
    localOffset=d.getTimezoneOffset()*60000; //获得当地时间偏移的毫秒数
    utc = localTime + localOffset; //utc即GMT时间
    offset =8; //以夏威夷时间为例,东10区
    hawaii = utc + (3600000*offset);
    return new Date(hawaii);
}
/**
 *转换日期对象为日期字符串
 * @param date 日期对象
 * @param isFull 是否为完整的日期数据,
 * 为true时, 格式如"2000-03-05 01:05:04"
 * 为false时, 格式如 "2000-03-05"
 * @return 符合要求的日期字符串
 */
function getSmpFormatDate(date, isFull) {
    var pattern = "";
    if (isFull == true || isFull == undefined) {
        pattern = "yyyy-MM-dd hh:mm:ss";
    } else {
        pattern = "yyyy-MM-dd";
    }
    return getFormatDate(date, pattern);
}
/**
 *
 */
function getSmpWFormatDate(date, isFull){
    var pattern = "";
    if (isFull == true || isFull == undefined) {
        pattern = "yyyy-MM-dd hh:mm:ss q";
    } else {
        pattern = "yyyy-MM-dd";
    }
    return getFormatDate(date, pattern);
}

/**
 *转换当前日期对象为日期字符串
 * @param date 日期对象
 * @param isFull 是否为完整的日期数据,
 * 为true时, 格式如"2000-03-05 01:05:04"
 * 为false时, 格式如 "2000-03-05"
 * @return 符合要求的日期字符串
 */
function getSmpFormatNowDate(isFull) {
    return getSmpFormatDate(new Date(), isFull);
}

/**
 *转换当前日期对象为日期字符串
 * @param date 日期对象
 * @param isFull 是否为完整的日期数据,
 * 为true时, 格式如"2000-03-05 01:05:04"
 * 为false时, 格式如 "2000-03-05"
 * @return 符合要求的日期字符串
 */
function getSmpFormatQDate(date , isFull) {
    return getSmpFormatDate(date, isFull);
}


/**
 * 前补位操作
 */
function indexString(data , lenght , string){
    var stringLenght = lenght-data.length;
    var stringData = "";
    if(stringLenght>0){//需要补位
        for(var i=1 ; i<=stringLenght ; i++){
            stringData+= string;
        }
        return stringData+data;
    }

}


/**
 *转换long值为日期字符串
 * @param l long值
 * @param isFull 是否为完整的日期数据,
 * 为true时, 格式如"2000-03-05 01:05:04"
 * 为false时, 格式如 "2000-03-05"
 * @return 符合要求的日期字符串
 */
function getSmpFormatDateByLong(l, isFull) {
    console.info(l);
    if(l==null || l=="" || l==0 || l=="0"){
        return "";
    }
    return getSmpFormatDate(DateFormat(l) , isFull);
}
/**
 *转换long值为日期字符串
 * @param l long值
 * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
 * @return 符合要求的日期字符串
 */
function getFormatDateByLong(l, pattern) {
    return getFormatDate(DateFormat(l), pattern);
}
/**
 *转换日期对象为日期字符串
 * @param l long值
 * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
 * @return 符合要求的日期字符串
 */
function getFormatDate(date, pattern) {
    if (date == undefined) {
        date = new Date();
    }
    if (pattern == undefined) {
        pattern = "yyyy-MM-dd hh:mm:ss";
    }
    return date.format(pattern);
}

function getFormataDate(date, pattern) {
    if (date == undefined) {
        date = new Date();
    }
    if (pattern == undefined) {
        pattern = "yyyy-MM";
    }
    return date.format(pattern);
}

/*'yyyy-MM-dd HH:mm:ss'格式的字符串转日期*/
function stringToDate(str){
    var tempStrs = str.split(" ");
    var dateStrs = tempStrs[0].split("-");
    var year = parseInt(dateStrs[0], 10);
    var month = parseInt(dateStrs[1], 10) - 1;
    var day = parseInt(dateStrs[2], 10);

    var date = new Date(year, month, day);
    return date;
}

var now = new Date();                    //当前日期
var nowDayOfWeek = now.getDay();         //今天本周的第几天
var nowDay = now.getDate();              //当前日
var nowMonth = now.getMonth();           //当前月
var nowYear = now.getYear();             //当前年
nowYear += (nowYear < 2000) ? 1900 : 0;  //

//格式化日期:yyyy-MM-dd
function formatDate(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth()+1;
    var myweekday = date.getDate();

    if(mymonth < 10){
        mymonth = "0" + mymonth;
    }
    if(myweekday < 10){
        myweekday = "0" + myweekday;
    }
    return (myyear+"-"+mymonth + "-" + myweekday);
}


//获得本周的开始日期
function getWeekStartDate() {
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
    return formatDate(weekStartDate);
}

//获得本周的结束日期
function getWeekEndDate() {
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
    return formatDate(weekEndDate);
}



直接引入common.js这个封装好的时间工具或者单独用stringToDate(str)这个方法。

function stringToDate(str){
		    var tempStrs = str.split(" ");
		    var dateStrs = tempStrs[0].split("-");
		    var year = parseInt(dateStrs[0], 10);
		    var month = parseInt(dateStrs[1], 10) - 1;
		    var day = parseInt(dateStrs[2], 10);

		    var date = new Date(year, month, day);
		    return date;
		}

首先因为后台返回来的时间是yyyy-mm-dd hh:mm:ss的字符串,所以先把字符串转成时间戳,然后才能调用getFullYear(),.getMonth()+1,.getDate(),.getDay(),.getHours(),.getMinutes(),.getSeconds()等。

然后再用方法转成对应的英文,比方说月份转成对应的英文:

$(function(){
	var m=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Spt","Oct","Nov","Dec");
	mn=stringToDate(json.date).getMonth();//json.date是从后台获取过来的字符串时间
	//alert(m[mn]);
			})

例子:

function stringToDate(str){
		    var tempStrs = str.split(" ");
		    var dateStrs = tempStrs[0].split("-");
		    var year = parseInt(dateStrs[0], 10);
		    var month = parseInt(dateStrs[1], 10) - 1;
		    var day = parseInt(dateStrs[2], 10);

		    var date = new Date(year, month, day);
		    return date;
		}

$(function(){
   
	$.each(json.info,function(i,e){
		var m=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Spt","Oct","Nov","Dec");
		var day=e.pubdate.split("-")[2];
		mn=stringToDate(e.pubdate).getMonth();
		$($(".news-lists article")[i]).find(".news-day").html(day.split(" ")[0]);//日
		$($(".news-lists article")[i]).find(".news-month").html(m[mn]);//月
	})
})

 

转载于:https://my.oschina.net/yanyaya/blog/1587799

你可能感兴趣的:(json,python)