How to Implement detailed time reminder in javascript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to implement detailed time reminder information in javascript, and involves the skills of javascript operation time, which has some reference value, for more information about how to implement detailed time reminder in javascript, see the example in this article. Share it with you for your reference. The details are as follows:

We often see user-friendly time prompts on social networks, such as what your friends updated a few minutes ago and what information your friends updated a few days ago.
These small tips shows a lot of human nature in a certain month. We can use different programs to achieve this effect. Below I will use the front-end javascript to achieve this effect.
This reduces the pressure on backend servers.

The javascript implementation code is as follows:

The Code is as follows:

// This function provides a more user-friendly time prompt
// @ Param date_str: the time passed by param date_str. The time format is as follows: 18:36:09
Function date_parser_diff_return (date_str ){
Var date = new Date ();
If (typeof (date_str )! = 'String') return date;
Var date_arr = date_str.split (new RegExp ("[: |-]", "ig "));
Var date_obj = new Date (date_arr [0], date_arr [1]-1, date_arr [2], date_arr [3], date_arr [4], date_arr [5]);
Var date_seconddiff = (new Date (). getTime ()-date_obj.getTime ()/1000;
Date_str_w = '';
If (date_seconddiff <60*30) date_str_w = Math. ceil (date_seconddiff/60) + "Minutes Ago ";
If (! Date_str_w & date_seconddiff <3600) date_str_w = "1 hour ago ";
If (! Date_str_w & date_seconddiff <3600*2) date_str_w = "2 hours ago ";
If (! Date_str_w & date_seconddiff <3600*3) date_str_w = "3 hours ago ";
If (! Date_str_w & date. getFullYear () = date_arr [0] & date. getMonth () = date_arr [1]-1 & date. getDate () = date_arr [2])
Date_str_w = "today" + date_arr [3] + ':' + date_arr [4];
If (! Date_str_w & date. getFullYear () = date_arr [0] & date. getMonth () = date_arr [1]-1 & date. getDate ()-1 = date_arr [2])
Date_str_w = "yesterday" + date_arr [3] + ':' + date_arr [4];
If (! Date_str_w & date. getFullYear () = date_arr [0] & date. getMonth () = date_arr [1]-1 & date. getDate ()-2 = date_arr [2])
Date_str_w = "the day before yesterday" + date_arr [3] + ':' + date_arr [4];
If (! Date_str_w & date. getFullYear () = date_arr [0] & date. getMonth () = date_arr [1]-1)
Date_str_w = (date. getMonth () + 1) + "month" + date_arr [2] + "Number" + date_arr [3] + ':' + date_arr [4];
If (! Date_str_w & date. getFullYear () = date_arr [0])
Date_str_w = "this year" + date_arr [1] + "month" + date_arr [2] + "no." + date_arr [3] + ':' + date_arr [4];
If (! Date_str_w & date. getFullYear ()-1 = date_arr [0])
Date_str_w = "last year" + date_arr [1] + "month" + date_arr [2] + "no." + date_arr [3] + ':' + date_arr [4];
Document. write (date_str_w );
};

I hope this article will help you design javascript programs.

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.