What are the web development technologies, html5 data visualization

Posted by sethadam1 on Sat, 15 Jan 2022 22:18:22 +0100

preface

Although there are mature and useful libraries for dealing with dates, such as momentjs and date FNS, sometimes we don't need the whole library in actual development. Therefore, I have sorted out various operations on date and time in front-end development, which is relatively complete. Some of them come from ourselves and some from our omnipotent netizens~

##Get current timestamp

var timestamp = Date.parse(new  Date()); //Accurate to seconds
var timestamp = (new Date()).valueOf();  //Accurate to milliseconds
var timestamp = new Date().getTime(); //Accurate to milliseconds
var timestamp = +new Date();
var timestamp = Date.now();

Gets the specified timestamp

var timestamp = (new Date(" 2019/10/24 08:00:00")).getTime();
var timestamp = (new Date(" 2019-10-24 08:00:00")).getTime();

##Gets the timestamp of the day before / after the current time

var timestamp = +new Date() - 24*60*60*1000;
var timestamp = +new Date() + 24*60*60*1000;

##Today zero timestamp

var timestamp = new Date(new Date().toLocaleDateString()).getTime();

##The timestamp of today's latest time 23:59:59

let timestamp = new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1;

##Gets the timestamp after n days of the current time

/**
 * @param {number} n Days
 * @returns {Number} The return value is the time in milliseconds
 */
function toNextTimes(n){
    let timestamp = +new Date() + n * 86400000;
    return timestamp;
}

First day of the week

/***
 *  @return {*} WeekFirstDay Returns the time of the first day of the week
 */
function showWeekFirstDay(){
    let Nowdate=new Date();
    let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    return WeekFirstDay;
}

##Last day of the week

/***
 *  @return {*} WeekLastDay Returns the time of the last day of the week
 */
function showWeekLastDay(){
    let Nowdate=new Date();
    let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    let WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
    return WeekLastDay;
}

##First day of the month

/***
 *  @return {*} MonthFirstDay Returns the time of the first day of the month
 */
function showMonthFirstDay(){
    let Nowdate=new Date();
    let MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth());
    return MonthFirstDay;
}

##Last day of the month

/***
 *  @return {*} MonthLastDay Returns the time of the last day of the month
 */
function showMonthLastDay(){
    let Nowdate=new Date();
    let MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1);
    let MonthLastDay=new Date(MonthNextFirstDay-86400000);
    return MonthLastDay;
}

##Date to time stamp

/**
 * @param {String} time - Date string, such as' 2018-8-8 ',' 2018,8,8 ',' 2018 / 8 / 8 '
 * @returns {Number} The return value is the time in milliseconds
 */
function timeToTimestamp (time) {
    let date = new Date(time);
    let timestamp = date.getTime();
    return timestamp;
}

##Format current time

/***
 *  @return {string} timeText Returns the system time string
 */
function getdataTimeSec() {
    let time = new Date();
    let weekDay;
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    //Get hours, minutes and seconds
    let h = time.getHours();
    let m = time.getMinutes();
    let s = time.getSeconds();
    //Check if it is less than 10
    h = check(h);
    m = check(m);
    s = check(s);
    let now_day = time.getDay();
    switch (now_day) {
        case 0: {
            weekDay = "Sunday"
        }
            break;
        case 1: {
            weekDay = "Monday"
        }
            break;
        case 2: {
            weekDay = "Tuesday"
        }
            break;
        case 3: {
            weekDay = "Wednesday"
        }
            break;
        case 4: {
            weekDay = "Thursday"
        }
            break;
        case 5: {
            weekDay = "Friday"
        }
            break;
        case 6: {
            weekDay = "Saturday"
        }
            break;
        case 7: {
            weekDay = "Sunday"
        }
            break;
    }
    let timeText = year + "year" + month + "month" + day + "day  " + " " + weekDay + " " + h + ":" + m +":" + s;

    return timeText
}

##Returns the time interval between specified timestamps

/**
 *  @param {*} startTime Timestamp of the start time
 *  @param {*} endTime Timestamp of the end time
 *  @return {string} str Return time string
 */
function getTimeInterval(startTime, endTime) {
    let runTime = parseInt((endTime - startTime) / 1000);
    let year = Math.floor(runTime / 86400 / 365);
    runTime = runTime % (86400 * 365);
    let month = Math.floor(runTime / 86400 / 30);
    runTime = runTime % (86400 * 30);
    let day = Math.floor(runTime / 86400);
    runTime = runTime % 86400;
    let hour = Math.floor(runTime / 3600);
    runTime = runTime % 3600;
    let minute = Math.floor(runTime / 60);
    runTime = runTime % 60;
    let second = runTime;
    let str = '';
    if (year > 0) {
        str = year + 'year';
    }
    if (year <= 0 && month > 0) {
        str = month + 'month';
    }
    if (year <= 0 && month <= 0 && day > 0) {
        str = day + 'day';
    }
    if (year <= 0 && month <= 0 && day <= 0 && hour > 0) {
        str = hour + 'hour';
    }
    if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute > 0) {
        str = minute + 'minute';
    }
    if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute <= 0 && second > 0) {
        str += second + 'second';
    }
    str += 'front';
    return str;
}

##Format date by type

/**
 * @param {*} date Specific date variable
 * @param {string} dateType Return type required
 * @return {string} dateText Returns a date string in the specified format
 */
function getFormatDate(date, dateType) {
    let dateObj = new Date(date);
    let month = dateObj.getMonth() + 1;
    let strDate = dateObj.getDate();
    let hours = dateObj.getHours();
    let minutes = dateObj.getMinutes();
    let seconds = dateObj.getSeconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;

    }
    if (hours >= 0 && hours <= 9) {
        hours = "0" + hours
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds
    }

    let dateText = dateObj.getFullYear() + 'year' + (dateObj.getMonth() + 1) + 'month' + dateObj.getDate() + 'day';
    if (dateType == "yyyy-mm-dd") {
        dateText = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate();
    }
    if (dateType == "yyyy.mm.dd") {
        dateText = dateObj.getFullYear() + '.' + (dateObj.getMonth() + 1) + '.' + dateObj.getDate();
    }
    if (dateType == "yyyy-mm-dd MM:mm:ss") {
        dateText = dateObj.getFullYear() + '-' + month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
    }
    if (dateType == "mm-dd MM:mm:ss") {
        dateText = month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
    }
    if (dateType == "yyyy year mm month dd day MM:mm:ss") {
        dateText = dateObj.getFullYear() + 'year' + month + 'month' + strDate + 'day' + ' ' + hours + ":" + minutes + ":" + seconds;
    }
    return dateText;
 }

Determine whether it is a leap year

/**
* @param  {number} year Year to judge
* @return {boolean} Returns a Boolean value
*/
function leapYear(year) {
    return !(year % (year % 100 ? 4 : 400));
}

##Returns the leap year between two years

/**
* @param  {number} start Start year
* @param  {number} end End year
* @return {array}  arr Returns an array that matches leap years
*/
function leapYears(start, end) {
    let arr = [];
    for (var i=start; i<end; i++) {
        if ( leapYear(i) ) {
            arr.push(i)
        }
    }
    return arr
}

##Determine whether the time format is valid

/**
* Short time, e.g. (10:24:06)
* @param  {string} str Short time to verify
* @return {boolean} Returns a Boolean value
*/
function isTime(str) {
    var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
    if (a == null) { return false; }
    if (a[1] >= 24 || a[3] >= 60 || a[4] >= 60) {
        return false
    }
    return true;
}

/**
* Short date, such as (October 24, 2019)
* @param  {string} str Short time to verify
* @return {boolean} Returns a Boolean value
*/
function strDateTime(str){
    var result = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (result == null) return false;
    var d = new Date(result[1], result[3] - 1, result[4]);
    return (d.getFullYear() == result[1] && d.getMonth() + 1 == result[3] && d.getDate() == result[4]);
}

/**
* Long date and time, such as (2019-10-24 10:24:06)
* @param  {string} str Short time to verify
* @return {boolean} Returns a Boolean value
*/
function strDateTime(str){
    var result = str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
    if (result == null) return false;
    var d = new Date(result[1], result[3] - 1, result[4], result[5], result[6], result[7]);
    return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4] && d.getHours() == result[5] && d.getMinutes() == result[6] && d.getSeconds() == result[7]);
}

##Verification size date

/**
* For example: "October 24, 2019" and "October 25, 2019"
* @param  {string} d1 Date to verify 1
* @param  {string} d2 Date to verify 2
* @return {boolean} Returns a Boolean value
*/
function compareDate(d1, d2) {
    return ((new Date(d1.replace(/-/g, "\/"))) < (new Date(d2.replace(/-/g, "\/"))));
}

##Verify if a date is today

/**
* @param  {string} val Date to verify
* @return {boolean} Returns a Boolean value
*/
function isToday(val){
    return new Date().toLocaleDateString() == new Date(val).toLocaleDateString();
}

##Verify that the date passed in is yesterday

/**
* @param  {string} val Date to verify
* @return {boolean} Returns a Boolean value
*/
function isYesterday(val) {
    var today = new Date();
    var yesterday = new Date(now - 1000 * 60 * 60 * 24);
    var test = new Date(val);
    if (yesterday.getYear() === test.getYear() && yesterday.getMonth() === test.getMonth() && yesterday.getDate() === test.getDate()) {
        return true;
    } else {
        return false;
    }
}

##Set the date after a few days

/**
* @param  {string} date Start date
* @param  {number} day Backward days
* @return {string} Return the desired date
*/
function convertDate (date, day) {
    let tempDate = new Date(date);
    tempDate.setDate(tempDate.getDate()+day);
    let Y = tempDate.getFullYear();
    let M = tempDate.getMonth()+1 < 10 ? '0'+(tempDate.getMonth()+1) : tempDate.getMonth()+1;
    let D = tempDate.getDate() < 10 ? '0'+(tempDate.getDate()) : tempDate.getDate();
    let result = Y + "-" + M + "-" + D
    return result;
}

Write at the end
If there are errors in the above functions or problems encountered in the work, but not written above, you are welcome to point them out~

last

It's not easy to code. If you think it's helpful, you can praise it and let more people in need see it

It's another job season. Here, I've prepared a set of selected high-frequency written interview questions for Java programmers to help you win BAT's offer. The questions range from primary Java foundation to advanced distributed architecture. A series of interview questions and answers are used for your reference. Here are some screenshots

Topics: Front-end Interview Programmer