基于jQuery的时间戳与日期间的转化
程序员文章站
2022-05-25 23:02:03
本文实例为大家分享了jquery时间戳与日期间的转化代码,供大家参考,具体内容如下
背景:
需求如图:
直接上代码,所有的内容都在注释里:...
本文实例为大家分享了jquery时间戳与日期间的转化代码,供大家参考,具体内容如下
背景:
需求如图:
直接上代码,所有的内容都在注释里:
/** * 格式化时间:补0操作 * */ function supplement(num){ if(parseint(num) < 10){ num = '0'+num; } return num; }; /** * 格式化时间:拓展jquery的全局变量 * */ $.extend({ jtime:{ //当前时间戳 秒:如果要毫秒就不除以1000 newtime: function(){ //本地时间然后在转为时间戳,没有时区区别 == date.now() return date.parse(new date())/1000; }, //日期格式(yy-mm-dd hh:mm:ss)转时间戳(秒) datetotamp: function(ostring) { var f = ostring.split(' ', 2); var d = (f[0] ? f[0] : '').split('-', 3); var t = (f[1] ? f[1] : '').split(':', 3); //使用date的构造函数,实力化并解析 return (new date( parseint(d[0], 10) || null, (parseint(d[1], 10) || 1) - 1, parseint(d[2], 10) || null, parseint(t[0], 10) || null, parseint(t[1], 10) || null, parseint(t[2], 10) || null )).gettime() / 1000; }, //时间戳(秒)转日期时间格式(yy-mm-dd [hh:mm:ss]):有条件的转(时间戳, 是否解析时间,时区:中国=8) tamptodate: function(unixtime, isfull, timezone) { //时区处理 if (typeof (timezone) === 'number'){ unixtime = parseint(unixtime) + parseint(timezone) * 60 * 60; } var time = new date(unixtime * 1000); var ymdhis = ""; ymdhis += time.getutcfullyear() + "-"; ymdhis += (time.getutcmonth()+1) + "-"; ymdhis += time.getutcdate(); //需要完整的就设置true if (isfull === true){ ymdhis += " " + time.getutchours() + ":"; ymdhis += time.getutcminutes() + ":"; ymdhis += time.getutcseconds(); } return ymdhis; }, //时间戳(毫秒)转日期时间格式 tamptodatetime: function (str) { var odate = new date(str), oyear = odate.getfullyear(), omonth = odate.getmonth()+1, oday = odate.getdate(), ohour = odate.gethours(), omin = odate.getminutes(), osen = odate.getseconds(), otime = oyear +'-'+ supplement(omonth) +'-'+ supplement(oday) +' '+ supplement(ohour) +':'+ supplement(omin) +':'+supplement(osen); //按格式拼接时间 return otime; } } });
原生的api:
interface date { /** returns a string representation of a date. the format of the string depends on the locale. */ tostring(): string; /** returns a date as a string value. */ todatestring(): string; /** returns a time as a string value. */ totimestring(): string; /** returns a value as a string value appropriate to the host environment's current locale. */ tolocalestring(): string; /** returns a date as a string value appropriate to the host environment's current locale. */ tolocaledatestring(): string; /** returns a time as a string value appropriate to the host environment's current locale. */ tolocaletimestring(): string; /** returns the stored time value in milliseconds since midnight, january 1, 1970 utc. */ valueof(): number; /** gets the time value in milliseconds. */ gettime(): number; /** gets the year, using local time. */ getfullyear(): number; /** gets the year using universal coordinated time (utc). */ getutcfullyear(): number; /** gets the month, using local time. */ getmonth(): number; /** gets the month of a date object using universal coordinated time (utc). */ getutcmonth(): number; /** gets the day-of-the-month, using local time. */ getdate(): number; /** gets the day-of-the-month, using universal coordinated time (utc). */ getutcdate(): number; /** gets the day of the week, using local time. */ getday(): number; /** gets the day of the week using universal coordinated time (utc). */ getutcday(): number; /** gets the hours in a date, using local time. */ gethours(): number; /** gets the hours value in a date object using universal coordinated time (utc). */ getutchours(): number; /** gets the minutes of a date object, using local time. */ getminutes(): number; /** gets the minutes of a date object using universal coordinated time (utc). */ getutcminutes(): number; /** gets the seconds of a date object, using local time. */ getseconds(): number; /** gets the seconds of a date object using universal coordinated time (utc). */ getutcseconds(): number; /** gets the milliseconds of a date, using local time. */ getmilliseconds(): number; /** gets the milliseconds of a date object using universal coordinated time (utc). */ getutcmilliseconds(): number; /** gets the difference in minutes between the time on the local computer and universal coordinated time (utc). */ gettimezoneoffset(): number; /** * sets the date and time value in the date object. * @param time a numeric value representing the number of elapsed milliseconds since midnight, january 1, 1970 gmt. */ settime(time: number): number; /** * sets the milliseconds value in the date object using local time. * @param ms a numeric value equal to the millisecond value. */ setmilliseconds(ms: number): number; /** * sets the milliseconds value in the date object using universal coordinated time (utc). * @param ms a numeric value equal to the millisecond value. */ setutcmilliseconds(ms: number): number; /** * sets the seconds value in the date object using local time. * @param sec a numeric value equal to the seconds value. * @param ms a numeric value equal to the milliseconds value. */ setseconds(sec: number, ms?: number): number; /** * sets the seconds value in the date object using universal coordinated time (utc). * @param sec a numeric value equal to the seconds value. * @param ms a numeric value equal to the milliseconds value. */ setutcseconds(sec: number, ms?: number): number; /** * sets the minutes value in the date object using local time. * @param min a numeric value equal to the minutes value. * @param sec a numeric value equal to the seconds value. * @param ms a numeric value equal to the milliseconds value. */ setminutes(min: number, sec?: number, ms?: number): number; /** * sets the minutes value in the date object using universal coordinated time (utc). * @param min a numeric value equal to the minutes value. * @param sec a numeric value equal to the seconds value. * @param ms a numeric value equal to the milliseconds value. */ setutcminutes(min: number, sec?: number, ms?: number): number; /** * sets the hour value in the date object using local time. * @param hours a numeric value equal to the hours value. * @param min a numeric value equal to the minutes value. * @param sec a numeric value equal to the seconds value. * @param ms a numeric value equal to the milliseconds value. */ sethours(hours: number, min?: number, sec?: number, ms?: number): number; /** * sets the hours value in the date object using universal coordinated time (utc). * @param hours a numeric value equal to the hours value. * @param min a numeric value equal to the minutes value. * @param sec a numeric value equal to the seconds value. * @param ms a numeric value equal to the milliseconds value. */ setutchours(hours: number, min?: number, sec?: number, ms?: number): number; /** * sets the numeric day-of-the-month value of the date object using local time. * @param date a numeric value equal to the day of the month. */ setdate(date: number): number; /** * sets the numeric day of the month in the date object using universal coordinated time (utc). * @param date a numeric value equal to the day of the month. */ setutcdate(date: number): number; /** * sets the month value in the date object using local time. * @param month a numeric value equal to the month. the value for january is 0, and other month values follow consecutively. * @param date a numeric value representing the day of the month. if this value is not supplied, the value from a call to the getdate method is used. */ setmonth(month: number, date?: number): number; /** * sets the month value in the date object using universal coordinated time (utc). * @param month a numeric value equal to the month. the value for january is 0, and other month values follow consecutively. * @param date a numeric value representing the day of the month. if it is not supplied, the value from a call to the getutcdate method is used. */ setutcmonth(month: number, date?: number): number; /** * sets the year of the date object using local time. * @param year a numeric value for the year. * @param month a zero-based numeric value for the month (0 for january, 11 for december). must be specified if numdate is specified. * @param date a numeric value equal for the day of the month. */ setfullyear(year: number, month?: number, date?: number): number; /** * sets the year value in the date object using universal coordinated time (utc). * @param year a numeric value equal to the year. * @param month a numeric value equal to the month. the value for january is 0, and other month values follow consecutively. must be supplied if numdate is supplied. * @param date a numeric value equal to the day of the month. */ setutcfullyear(year: number, month?: number, date?: number): number; /** returns a date converted to a string using universal coordinated time (utc). */ toutcstring(): string; /** returns a date as a string value in iso format. */ toisostring(): string; /** used by the json.stringify method to enable the transformation of an object's data for javascript object notation (json) serialization. */ tojson(key?: any): string; } interface dateconstructor { new(): date; new(value: number): date; new(value: string): date; new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): date; (): string; readonly prototype: date; /** * parses a string containing a date, and returns the number of milliseconds between that date and midnight, january 1, 1970. * @param s a date string */ parse(s: string): number; /** * returns the number of milliseconds between midnight, january 1, 1970 universal coordinated time (utc) (or gmt) and the specified date. * @param year the full year designation is required for cross-century date accuracy. if year is between 0 and 99 is used, then year is assumed to be 1900 + year. * @param month the month as an number between 0 and 11 (january to december). * @param date the date as an number between 1 and 31. * @param hours must be supplied if minutes is supplied. an number from 0 to 23 (midnight to 11pm) that specifies the hour. * @param minutes must be supplied if seconds is supplied. an number from 0 to 59 that specifies the minutes. * @param seconds must be supplied if milliseconds is supplied. an number from 0 to 59 that specifies the seconds. * @param ms an number from 0 to 999 that specifies the milliseconds. */ utc(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number; now(): number; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: php链式操作的实现方式分析