Recently need to implement the function of the topic, because my timestamp is generated by PHP, so the conversion JS timestamp needs to multiply 1000, nonsense not to say, see the code below!
You can judge whether the value passed in is a numeric type, and whether it is larger than the current timestamp! Can be prompted according to the results of illegal parameters and so on!
1 functionGetdatediff (datetimestamp) {2 varminute = 1000 * 60;3 varhour = minute * 60;4 varDay = Hour * 24;5 varHalfamonth = Day * 15;6 varMonth = Day * 30;7 8 varnow =NewDate (). GetTime ();9 varDiffvalue = Now-Datetimestamp;Ten One varMONTHC =diffvalue/month; A varWEEKC =diffvalue/(7*day); - varDAYC =diffvalue/day; - varHOURC =diffvalue/hour; the varMinC =diffvalue/minute; - if(hourc>=12){ -result = "Over"; - } + Else if(hourc>=1){ -result = parseint (HOURC) + "hours ago"; + } A Else if(minc>=1){ atresult = parseint (MinC) + "Minutes ago"; -}Else -result = "just published"; - returnresult; - } - in varTime = Getdatediff (' 1466555123000 '); -alert (time);
JS implementation How many hours ago, how many days ago ...