如何用js获取特定时间戳

如何用js获取特定时间戳,第1张

var formatTime = function(time = new Date(), format) {

    const TOTOW = e => `0${e}`substr(-2); // 转成2位的格式  1 => 01

    const date = new Date(time);

    const yyyy = dategetFullYear();

    const MM = TOTOW(dategetMonth() + 1);

    const dd = TOTOW(dategetDate());

    const hh = TOTOW(dategetHours());

    const mm = TOTOW(dategetMinutes());

    const ss = TOTOW(dategetSeconds());

    let result;

    if (format) {

      result = formatreplace(/yyyy/i, yyyy)replace(/MM/, MM)replace(/dd/i, dd)replace(/hh/i, hh)replace(/mm/, mm)replace(/ss/i, ss);

    } else {

      result = `${yyyy}-${MM}-${dd} ${hh}:${mm}:${ss}`;

    }

    return result;

}

setInterval(() => {

let now = formatTime(new Date(), 'yyyy年MM月dd日 hh时mm分ss秒'); // 月份必须是大写MM,分钟必须是小写mm,其他大小写都行

documentbodyinnerText = now;

}, 1000)

var bd = parseInt((new Date() - new Date(出生年,月,日,时,分,秒))/1000);  //出生至今总秒数

parseInt(bd/24/60/60)   //出生至今的天数

parseInt((bd%(246060))/60/60)   //时

parseInt((bd%(6060))/60)   //分

bd%60   //秒

// JavaScript Document by Youzi, send email to youzi_friend@163com to connect author

///////////////////////////////////////////////// showDate Start

windowonload = function () {

showDate();

}

// Date Display Start

function showDate(){

var today = new Date();

var nowYear = todaygetFullYear();

var nowMonth = checkTime(todaygetMonth() + 1);

var nowDate = checkTime(todaygetDate());

var nowHour = checkTime(todaygetHours());

var nowMin = checkTime(todaygetMinutes());

var nowSec = checkTime(todaygetSeconds());

var weekday = new Array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');

var nowDay = weekday[todaygetDay()];

/ 农历类调用 /

var date = new ChineseCalendar();

var chinaDate = datesolarDay2();

var dateDiv = documentgetElementById("showDate");

var dateTxt = nowYear +"年"+ nowMonth +"月"+ nowDate +"日 " + chinaDate +" "+ nowDay +" "+ nowHour +":"+ nowMin +":"+ nowSec;

if ((nowMonth == 02 && nowDate == 10) || chinaDate == "农历正月初一") {

dateTxt += "<br /> IT's BenBen's birthday today <br /> <strong>Happay birthday to BenBen</strong>!";

}

dateDivinnerHTML = dateTxt;

setTimeout('showDate()',500);

}

function checkTime(i){

if (i < 10) { i = "0" + i; }

return i;

}

//农历类

function ChineseCalendar(dateObj){

thisdateObj = (dateObj != undefined) dateObj : new Date();

thisSY = thisdateObjgetFullYear();

thisSM = thisdateObjgetMonth();

thisSD = thisdateObjgetDate();

thislunarInfo = [0x04bd8,0x04ae0,0570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,

0x04ae0,05b6,04d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0da2,0x095b0,0x14977,

0x04970,04b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,

0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,

0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0950,0x0b557,

0x06ca0,0x0b550,0x15355,0x04da0,05b0,0x14573,0x052b0,09a8,0x0e950,0x06aa0,

0ea6,0b50,0x04b60,0ae4,0570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,

0x096d0,0x04dd5,0x04ad0,04d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,

0x095b0,0x049b0,0974,04b0,0x0b27a,0x06a50,0x06d40,0f46,0b60,0x09570,

0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0b60,0x096d5,0x092e0,

0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0bb7,0x025d0,0x092d0,0x0cab5,

0950,0x0b4a0,0x0baa4,0d50,0x055d9,0x04ba0,05b0,0x15176,0x052b0,0930,

0x07954,0x06aa0,0d50,0x05b52,0x04b60,06e6,04e0,0x0d260,0x0ea65,0x0d530,

0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,04d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,

0x0b5a0,0x056d0,0x055b2,0x049b0,0577,04b0,0a50,0x1b255,0x06d20,0da0,

0x14b63];

//传回农历 y年闰哪个月 1-12 , 没闰传回 0

thisleapMonth = function(y){

return thislunarInfo[y - 1900] & 0xf;

};

//传回农历 y年m月的总天数

thismonthDays = function(y, m){

return (thislunarInfo[y - 1900] & (0x10000 >> m)) 30 : 29;

};

//传回农历 y年闰月的天数

thisleapDays = function(y){

if (thisleapMonth(y)) {

return (thislunarInfo[y - 1900] & 0x10000) 30 : 29;

}

else {

return 0;

}

};

//传回农历 y年的总天数

thislYearDays = function(y){

var i, sum = 348;

for (i = 0x8000; i > 0x8; i >>= 1) {

sum += (thislunarInfo[y - 1900] & i) 1 : 0;

}

return sum + thisleapDays(y);

};

//算出农历, 传入日期对象, 传回农历日期对象

//该对象属性有 year month day isLeap yearCyl dayCyl monCyl

thisLunar = function(dateObj){

var i, leap = 0, temp = 0, lunarObj = {};

var baseDate = new Date(1900, 0, 31);

var offset = (dateObj - baseDate) / 86400000;

lunarObjdayCyl = offset + 40;

lunarObjmonCyl = 14;

for (i = 1900; i < 2050 && offset > 0; i++) {

temp = thislYearDays(i);

offset -= temp;

lunarObjmonCyl += 12;

}

if (offset < 0) {

offset += temp;

i--;

lunarObjmonCyl -= 12;

}

lunarObjyear = i;

lunarObjyearCyl = i - 1864;

leap = thisleapMonth(i);

lunarObjisLeap = false;

for (i = 1; i < 13 && offset > 0; i++) {

if (leap > 0 && i == (leap + 1) && lunarObjisLeap == false) {

--i;

lunarObjisLeap = true;

temp = thisleapDays(lunarObjyear);

}

else {

temp = thismonthDays(lunarObjyear, i)

}

if (lunarObjisLeap == true && i == (leap + 1)) {

lunarObjisLeap = false;

}

offset -= temp;

if (lunarObjisLeap == false) {

lunarObjmonCyl++;

}

}

if (offset == 0 && leap > 0 && i == leap + 1) {

if (lunarObjisLeap) {

lunarObjisLeap = false;

}

else {

lunarObjisLeap = true;

--i;

--lunarObjmonCyl;

}

}

if (offset < 0) {

offset += temp;

--i;

--lunarObjmonCyl

}

lunarObjmonth = i;

lunarObjday = offset + 1;

return lunarObj;

};

//中文日期

thiscDay = function(m, d){

var nStr1 = ['日', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];

var nStr2 = ['初', '十', '廿', '卅', ' '];

var s;

if (m > 10) {

s = '十' + nStr1[m - 10];

}

else {

s = nStr1[m];

}

s += '月';

switch (d) {

case 10:

s += '初十';

break;

case 20:

s += '二十';

break;

case 30:

s += '三十';

break;

default:

s += nStr2[Mathfloor(d / 10)];

s += nStr1[d % 10];

}

return s;

};

thissolarDay2 = function(){

var sDObj = new Date(thisSY, thisSM, thisSD);

var lDObj = thisLunar(sDObj);

var tt = '农历' + thiscDay(lDObjmonth, lDObjday);

lDObj = null;

return tt;

};

thisweekday = function(){

var day = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];

return day[thisdateObjgetDay()];

};

thisYYMMDD = function(){

var dateArr = [thisSY, '年', thisSM + 1, '月', thisSD, '日'];

return dateArrjoin('');

}

}

网上找的,年月日,时分秒,星期,农历都有,你参考一下

/ 农历类用法: 源地址: >

<script>

function show(){

var date = new Date(); //日期对象

var now = "";

now = dategetFullYear()+"年"; //读英文就行了

now = now + (dategetMonth()+1)+"月"; //取月的时候取的是当前月-1如果想取当前月+1就可以了

now = now + dategetDate()+"日";

now = now + dategetHours()+"时";

now = now + dategetMinutes()+"分";

now = now + dategetSeconds()+"秒";

documentgetElementById("nowDiv")innerHTML = now; //div的html是now这个字符串

setTimeout("show()",1000); //设置过1000毫秒就是1秒,调用show方法

}

</script>

<body onload="show()"> <!-- 网页加载时调用一次 以后就自动调用了-->

<div id="nowDiv"></div>

</body>

直接复制就可以看效果了

以上就是关于如何用js获取特定时间戳全部的内容,包括:如何用js获取特定时间戳、用js在控制台实现计算以下,具体到秒、用javascript获取当前的时间 精确到秒 哪个高手 给个答案等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9876151.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存