本文實(shí)例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下
將時(shí)間戳轉(zhuǎn)換為日期格式
var date = new Date(時(shí)間戳);獲取一個(gè)時(shí)間對象。下面是獲取時(shí)間日期的方法下面是獲取時(shí)間日期的方法。date.getFullYear();//獲取完整的年份(4位,1970)date.getMonth();//獲取月份(0-11,0代表1月,用的時(shí)候記得加上1)date.getDate();//獲取日(1-31)date.getTime();//獲取時(shí)間(從1970.1.1開始的毫秒數(shù))date.getHours();//獲取小時(shí)數(shù)(0-23)date.getMinutes();//獲取分鐘數(shù)(0-59)date.getSeconds();//獲取秒數(shù)(0-59)
比如我需要 2015-8-24_8-24-30這樣的格式
function formatDate(datetime) { var year = datetime.getFullYear(), month = (datetime.getMonth() + 1 < 10) ? '0' + (datetime.getMonth() + 1):datetime.getMonth() + 1, day = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate(), hour = datetime.getHours() < 10 ? '0' + datetime.getHours() : datetime.getHours(), min = datetime.getMinutes() < 10 ? '0' + datetime.getMinutes() : datetime.getMinutes(), sec = datetime.getSeconds() < 10 ? '0' + datetime.getSeconds() : datetime.getSeconds(); return year + '-' + month + '-' + day + '_' + hour + '-' + min + '-' + sec;}這里datatime是Date對象,格式化 datetime = new Date(time); time是時(shí)間戳。
將日期格式轉(zhuǎn)換為時(shí)間戳
var strtime = '2014-04-23 18:55:49:123';var date = new Date(strtime); //傳入一個(gè)時(shí)間格式,如果不傳入就是獲取現(xiàn)在的時(shí)間了,這樣做不兼容火狐。// 可以這樣做var arr = strtime.replace(/ |:/g, '-').split('-');date = new Date(Date.UTC(arr[1], arr[2], arr[3], arr[4], arr[5]));//三種方式獲取:time1 = date.getTime();time2 = date.valueOf();time3 = Date.parse(date);//三種獲取的區(qū)別:第一、第二種:會(huì)精確到毫秒第三種:只能精確到秒,毫秒將用0來代替比如上面代碼輸出的結(jié)果(一眼就能看出區(qū)別): 1398250549123 1398250549123 1398250549000 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答