JavaScript implementation of detailed time reminding information effect _javascript tips

Source: Internet
Author: User
Tags getdate

This article gives an example of how JavaScript implements a detailed time reminder information effect. Share to everyone for your reference. Specifically as follows:

We often see a very human time cue on social networks, such as what your friend updated a few minutes ago, and what information your friends updated a few days ago.
These tips are a lot more humane than just showing a certain month. We can use different programs to achieve this effect. Here's what I do with the foreground JavaScript.
This can reduce the pressure on the backend server.

The JavaScript implementation code is as follows:

Copy Code code as follows:
This function implements a more humane time hint
@param date_str pass over time, time format such as: 2010-12-14 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 hours 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]+ "+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 with your JavaScript programming.

Related Article

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.