JS Custom Format timestamp format

Source: Internet
Author: User

The topic is: Write a module, external call this module, request parameter is timestamp, module requirements
Today's time, unified with 24 hours writing 03:00, 15:04
Yesterday's time, unified writing yesterday
Time before yesterday, but within this week's time, unified by Monday, Tuesday, Wednesday so to write
Last week, unified collaboration 15/3/4, followed by the year/month/day
Note that the month and day are single digit, do not need to use 0 stations to become two bits, other display null

function Formattime (time) {Const __TIME = new Date (time *) function zeroize (num) {return (String (num). length = = 1? ' 0 ': ') + num} function formatTime1 () {//translates into time division seconds return ' ${zeroize (__time.gethours ())}:${zeroize (__time.getminutes ())}:${zeroize (__time.getseconds ())} '} function FormatTime2 () {//Convert to Month day return ' ${__time.getfullyear (). ToString (). s Ubstr (2,4)}/${__time.getmonth () +1}/${__time.getdate ()} '} function Getweek () {Const _DAY = new Date (time *). Get      Day () switch (_day) {case 0:return ' days ' case 1:return ' one ' case 2:return ' two '    Case 3:return ' Three ' case 4:return ' four ' case 5:return ' five ' case 6:return ' six '  }} var now = ' ${new date (). getFullYear ()}/${new date (). GetMonth () +1}/${new date (). GetDate ()} 00:00:00 ' var _now = new Date (now). GetTime ()//timestamp than today at 0 O ' Day is yesterday if (time * < _now && time * > _NOW-24 * 60 * 60 * 100 0) {return ' Yesterday${formattime1 ()} '}//timestamp greater than today 0 o'clock is today if (time * > _now) {return ' Today ${formattime1 ()} '}//timestamp is less than yesterday's 0 points greater than this Monday The inter-stamp is this week if (time * < _now-24 * * * * * * * * && * * * * * * * * * * * * * * * * * * * * * * * * (new Date) ( ). GetDay ()-1) {return ' Week ${getweek ()} '}//timestamp is less than the timestamp of the previous Monday timestamp is last week if (time * < _NOW-24 * 60 * 60 * 10 XX * (new Date (). GetDay ()-1) && time * $ > _now-24 * * * * * * * * new Date (). GetDay ()-6 * 24 * 60 * * +) {return formatTime2 ()} return ' null '}

JS custom format timestamp format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.