5 examples of 'display current date and time in html' in JavaScript

Every line of 'display current date and time in html' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
20function displayDateContent(startDateTime, endDateTime) {
21 if (endDateTime) {
22 return (
23 `${startDateTime.date} ${startDateTime.monthSym} ${startDateTime.year} - ` +
24 `${endDateTime.date} ${endDateTime.monthSym} ${endDateTime.year}`
25 );
26 }
27 return `${startDateTime.date} ${startDateTime.monthSym} ${startDateTime.year}`;
28}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

37function displayDateTime(time) {
38 if (time)
39 return time.replace('T', ' ').replace('.000Z', '');
40 else
41 return time;
42}
90function longdate(date, showtime) {
91 return format_date(date, months, showtime)
92}
31renderDate(pubTime, fromSite) {
32
33 // 时间差的计算
34 let minute = 1000 * 60; // 1分钟
35 let hour = minute * 60; // 1小时
36 let day = hour * 24; // 1天
37 let week = day * 7; // 1周
38 let month = day * 30; // 1个月
39
40 // 计算时间差
41 let now = new Date().getTime(); // 获取当前时间
42 let diffValue = now - Date.parse(pubTime.replace(/-/gi, "/"));
43
44 if (diffValue < 0) return;
45
46 let monthC = diffValue/month; // 相差了几个月
47 let weekC = diffValue/week; // 相差几周
48 let dayC = diffValue/day; // 相差几天
49 let hourC = diffValue/hour; // 相差几小时
50 let minuteC = diffValue/minute; // 相差几分钟
51
52 let result;
53
54 if (monthC >= 1) {
55 result = parseInt(monthC) + "月前";
56 }else if (weekC >= 1) {
57 result = parseInt(weekC) + "周前";
58 }else if (dayC >= 1) {
59 result = parseInt(dayC) + "天前";
60 }else if (hourC >= 1) {
61 result = parseInt(hourC) + "小时前";
62 }else if (minuteC >= 1) {
63 result = parseInt(minuteC) + "分钟前";
64 }else result = "刚刚";
65
66 return result + ' · ' + fromSite; // 拼接
67
68}
157function makeHTML(tw) {
158 function d2(dig) { return (dig>9?"":"0") + dig }
159 var d = new Date(tw.created_at);
160 d = d.getDate() + "日 " + d.getHours() + ":" + d2(d.getMinutes()) + ":" + d2(d.getSeconds());
161 //ユーザアイコン
162 return (tw.user.url ? '<a target="twitter" href="'+tw.user.url+'">' : '') +
163 '<img border="0" align="left" width="24" height="24" alt="' + tw.user.name +
164 '" src="' + tw.user.profile_image_url + '">' + (tw.user.url ? '</a>' : '') +
165 //fav
166 '<img align="right" src="http://assets3.twitter.com/images/icon_star_'+(tw.favorited?'full':'empty')+'.gif" ' +
167 'onClick="fav(this,' + tw.id + ')">' +
168 //名前(Twitter本家のリンク)
169 '<a target="twitter" href="http://twitter.com/' + tw.user.screen_name + '">' + tw.user.screen_name +
170 /*プロフィールの名前*/ (tw.user.name!=tw.user.screen_name ? '('+tw.user.name+')' : '') + '</a>' +
171 /* protected? */ (tw.user.protected ? '<img src="http://assets0.twitter.com/images/icon_red_lock.gif">' : '') +
172 //本文クリックで@追加
173 " <span onClick=\"replyTo('" + tw.user.screen_name + "'," + tw.id + ")\" class=\"status\">" +
174 //本文 (https〜をリンクに置換 + @をtwitter本家へのリンクに置換)
175 tw.text.replace(/(https?:\/\/[^ ]*)/g, " <a onClick=\"event.stopPropagation();\" target=\"_blank\" href=\"$1\">$1</a>")
176 .replace(/@([0-9A-Za-z_\-]+)/g, "<a onClick=\"event.stopPropagation();\" target=\"twitter\" " +
177 "href=\"http://twitter.com/$1\">@$1</a>") + '</span>' +
178 //日付
179 '<span class="date">' + d + '</span>' +
180 //返信元へのリンク
181 (tw.in_reply_to_status_id ? ' <a class="inreply" href="#" onClick="dispReply('+tw.in_reply_to_status_id+',this);return false;">☞</a>' : '') +
182 '<br clear="left">';
183}

Related snippets