js时间戳怎么转成日期格式

JavaScript030

js时间戳怎么转成日期格式,第1张

什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。Unix时间戳不仅被使用在Unix系统、类Unix系统中,也在许多其他操作系统中被广泛采用。

目前相当一部分操作系统使用32位二进制数字表示时间。此类系统的Unix时间戳最多可以使用到格林威治时间2038年01月19日03时14分07秒(二进制:01111111 11111111 11111111 11111111)。其后一秒,二进制数字会变为10000000 00000000 00000000 00000000,发生溢出错误,造成系统将时间误解为1901年12月13日20时45分52秒。这很可能会引起软件故障,甚至是系统瘫痪。使用64位二进制数字表示时间的系统(最多可以使用到格林威治时间292,277,026,596年12月04日15时30分08秒)则基本不会遇到这类溢出问题。

一.js将时间转换成时间戳

1.js获取当前时间戳的方法

var timestamp1 = Date.parse(new Date())

var timestamp2 = (new Date()).valueOf()

var timestamp3 = new Date().getTime()

第一种:获取的时间戳是把毫秒改成000显示,第二种和第三种是获取了当前毫秒的时间戳。

2.js获取制定时间戳的方法

var oldTime = (new Date("2015/06/23 08:00:20")).getTime()/1000

getTime()返回数值的单位是毫秒。

二.js把时间戳转为为普通日期格式

1.Date toLocaleString方法

function getLocalTime(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ')

}

parseInt() 函数可解析一个字符

function RunGLNL(){ var today=new Date()var d=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六")var DDDD=(today.getYear()<100 ? today.getYear()+1900:today.getYear())+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日"DDDD = DDDD + " " + d[today.getDay()]DDDD = DDDD+ " " + (CnDateofDateStr(today))//DDDD = DDDD+ " " + SolarTerm(today)document.write(DDDD)} function DaysNumberofDate(DateGL){ return parseInt((Date.parse(DateGL)-Date.parse(DateGL.getYear()+"/1/1"))/86400000)+1} function CnDateofDate(DateGL){ var CnData=new Array( 0x16,0x2a,0xda,0x00,0x83,0x49,0xb6,0x05,0x0e,0x64,0xbb,0x00,0x19,0xb2,0x5b,0x00, 0x87,0x6a,0x57,0x04,0x12,0x75,0x2b,0x00,0x1d,0xb6,0x95,0x00,0x8a,0xad,0x55,0x02, 0x15,0x55,0xaa,0x00,0x82,0x55,0x6c,0x07,0x0d,0xc9,0x76,0x00,0x17,0x64,0xb7,0x00, 0x86,0xe4,0xae,0x05,0x11,0xea,0x56,0x00,0x1b,0x6d,0x2a,0x00,0x88,0x5a,0xaa,0x04, 0x14,0xad,0x55,0x00,0x81,0xaa,0xd5,0x09,0x0b,0x52,0xea,0x00,0x16,0xa9,0x6d,0x00, 0x84,0xa9,0x5d,0x06,0x0f,0xd4,0xae,0x00,0x1a,0xea,0x4d,0x00,0x87,0xba,0x55,0x04 )var CnMonth=new Array()var CnMonthDays=new Array()var CnBeginDayvar LeapMonthvar Bytes=new Array()var Ivar CnMonthDatavar DaysCountvar CnDaysCountvar ResultMonthvar ResultDayvar yyyy=DateGL.getYear()var mm=DateGL.getMonth()+1var dd=DateGL.getDate()if(yyyy<100) yyyy+=1900if ((yyyy <1997) || (yyyy >2020)){ return 0} Bytes[0] = CnData[(yyyy - 1997) * 4]Bytes[1] = CnData[(yyyy - 1997) * 4 + 1]Bytes[2] = CnData[(yyyy - 1997) * 4 + 2]Bytes[3] = CnData[(yyyy - 1997) * 4 + 3]if ((Bytes[0] &0x80) != 0) {CnMonth[0] = 12} else {CnMonth[0] = 11} CnBeginDay = (Bytes[0] &0x7f)CnMonthData = Bytes[1]CnMonthData = CnMonthData <<8CnMonthData = CnMonthData | Bytes[2]LeapMonth = Bytes[3]for (I=15I>=0I--){ CnMonthDays[15 - I] = 29if (((1 <<I) &CnMonthData) != 0 ){ CnMonthDays[15 - I]++} if (CnMonth[15 - I] == LeapMonth ){ CnMonth[15 - I + 1] = - LeapMonth} else{ if (CnMonth[15 - I] <0 ){CnMonth[15 - I + 1] = - CnMonth[15 - I] + 1} else {CnMonth[15 - I + 1] = CnMonth[15 - I] + 1} if (CnMonth[15 - I + 1] >12 ){ CnMonth[15 - I + 1] = 1} } } DaysCount = DaysNumberofDate(DateGL) - 1if (DaysCount <= (CnMonthDays[0] - CnBeginDay)){ if ((yyyy >1901) &&(CnDateofDate(new Date((yyyy - 1)+"/12/31")) <0)){ ResultMonth = - CnMonth[0]} else {ResultMonth = CnMonth[0]} ResultDay = CnBeginDay + DaysCount} else{ CnDaysCount = CnMonthDays[0] - CnBeginDayI = 1while ((CnDaysCount <DaysCount) &&(CnDaysCount + CnMonthDays[I] <DaysCount)){ CnDaysCount+= CnMonthDays[I]I++} ResultMonth = CnMonth[I]ResultDay = DaysCount - CnDaysCount} if (ResultMonth >0){ return ResultMonth * 100 + ResultDay} else{return ResultMonth * 100 - ResultDay} } function CnYearofDate(DateGL){ var YYYY=DateGL.getYear()var MM=DateGL.getMonth()+1var CnMM=parseInt(Math.abs(CnDateofDate(DateGL))/100)if(YYYY<100) YYYY+=1900if(CnMM>MM) YYYY--YYYY-=1864return CnEra(YYYY)+"年"} function CnMonthofDate(DateGL){ var CnMonthStr=new Array("零","正","二","三","四","五","六","七","八","九","十","十一","腊")var MonthMonth = parseInt(CnDateofDate(DateGL)/100)if (Month <0){return "闰" + CnMonthStr[-Month] + "月"} else{return CnMonthStr[Month] + "月"} } function CnDayofDate(DateGL){ var CnDayStr=new Array("零", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十")var DayDay = (Math.abs(CnDateofDate(DateGL)))%100return CnDayStr[Day]} function DaysNumberofMonth(DateGL){ var MM1=DateGL.getYear()MM1<100 ? MM1+=1900:MM1var MM2=MM1MM1+="/"+(DateGL.getMonth()+1)MM2+="/"+(DateGL.getMonth()+2)MM1+="/1"MM2+="/1"return parseInt((Date.parse(MM2)-Date.parse(MM1))/86400000)} function CnEra(YYYY){ var Tiangan=new Array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸")//var Dizhi=new Array("子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)","巳(蛇)", //"午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)")var Dizhi=new Array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥")return Tiangan[YYYY%10]+Dizhi[YYYY%12]} function CnDateofDateStr(DateGL){ if(CnMonthofDate(DateGL)=="零月") return " 请调整您的计算机日期!"else return "农历:"+CnYearofDate(DateGL)+ " " + CnMonthofDate(DateGL) + CnDayofDate(DateGL)} function SolarTerm(DateGL){ var SolarTermStr=new Array( "小寒","大寒","立春","雨水","惊蛰","春分", "清明","谷雨","立夏","小满","芒种","夏至", "小暑","大暑","立秋","处暑","白露","秋分", "寒露","霜降","立冬","小雪","大雪","冬至")var DifferenceInMonth=new Array( 1272060,1275495,1281180,1289445,1299225,1310355, 1321560,1333035,1342770,1350855,1356420,1359045, 1358580,1355055,1348695,1340040,1329630,1318455, 1306935,1297380,1286865,1277730,1274550,1271556)var DifferenceInYear=31556926var BeginTime=new Date(1901/1/1)BeginTime.setTime(947120460000)for(DateGL.getYear()<BeginTime.getYear()){ BeginTime.setTime(BeginTime.getTime()-DifferenceInYear*1000)} for(DateGL.getYear()>BeginTime.getYear()){ BeginTime.setTime(BeginTime.getTime()+DifferenceInYear*1000)} for(var M=0DateGL.getMonth()>BeginTime.getMonth()M++){ BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000)} if(DateGL.getDate()>BeginTime.getDate()){ BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000)M++} if(DateGL.getDate()>BeginTime.getDate()){ BeginTime.setTime(BeginTime.getTime()+DifferenceInMonth[M]*1000)M==23?M=0:M++} var JQif(DateGL.getDate()==BeginTime.getDate()){ JQ=" 今天是<font color='#FF9999'><b>"+SolarTermStr[M] + "</b></font>"} else if(DateGL.getDate()==BeginTime.getDate()-1){ JQ=" 明天是<font color='#FF9999'><b>"+SolarTermStr[M] + "</b></font>"} else if(DateGL.getDate()==BeginTime.getDate()-2){ JQ=" 后天是<font color='#FF9999'><b>"+SolarTermStr[M] + "</b></font>"} else{ JQ=" " if(DateGL.getMonth()==BeginTime.getMonth()){ JQ+=" 本月"} else{ JQ+=" 下月"} JQ+=BeginTime.getDate()+"日"+"<font color='#FF9999'><b>"+SolarTermStr[M]+"</b></font>"} return JQ} function CAL() {} RunGLNL() 详细出处参考: http://www.jb51.net/article/17021.htm

其实我也不懂这个算法,网上粘贴的,我觉得可以不去深究这个公式,反正有现成代码:

设:公元年数-1977(或1901)=4Q+R

则:阴历日期=14Q+10.6(R+1)+年内日期序数-29.5n

(注:式中Q、R、n均为自然数,R<4)

例:1994年5月7日的阴历日期为:

1994-1977=17=4×4+1

故:Q=4,R=1 则:5月7日的阴历日期为:

14×4+10.6(1+1)+(31+28+31+30+7)-29.5n

=204.2- 29.5n

然后用29.5去除204.2得商数6......27.2,6即是n值,余数27即是阴历二十七日。