Skip to content

Instantly share code, notes, and snippets.

@hmel1990
Created May 16, 2025 10:45
Show Gist options
  • Save hmel1990/82a9e4e37c81393409ba2b91d66dd472 to your computer and use it in GitHub Desktop.
Save hmel1990/82a9e4e37c81393409ba2b91d66dd472 to your computer and use it in GitHub Desktop.
NewsClassDZ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Создание класса "Новости"</title>
</head>
<body>
<div id="total"></div>
<style>
body
{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: wheat;
padding: 2rem;
margin: 2rem auto;
height: 200px;
width: 600px;
border: gray 5px solid;
}
</style>
<script>
class News {
title;
text;
arrayOfTags;
publicationDate;
constructor(title, text, arrayOfTags, publicationDate) {
this.title = title;
this.text = text;
this.arrayOfTags = arrayOfTags;
this.publicationDate = publicationDate;
}
printWithHtmlDiv() {
const container = document.getElementById("total");
container.innerHTML = "";
const itemTitle = document.createElement("div");
itemTitle.className = "title";
itemTitle.innerHTML = `${this.title}`;
container.appendChild(itemTitle);
//...
}
print ()
{
const now = new Date(); //Date возвращает время в миллисекундах
const pubDate = new Date(this.publicationDate);
// Рассчитываем разницу в днях
const msInDay = 1000 * 60 * 60 * 24;//количество миллисекунд в сутках
const diffDays = Math.floor((now - pubDate) / msInDay); // разница в днях
let formattedDate = "";
if (diffDays === 0)
{
formattedDate = "сегодня";
}
else if (diffDays < 7)
{
formattedDate = `${diffDays} дней назад`;
}
else
{
const day = String(pubDate.getDate()).padStart(2, '0');
const month = String(pubDate.getMonth() + 1).padStart(2, '0');
const year = pubDate.getFullYear();
formattedDate = `${day}.${month}.${year}`;
}
document.write(`<div style="color: black; font-weight: bold; font-size: 1.2rem;">${this.title}</div>`)
document.write(`<div style="color: black; font-weight: bold;font-size: 0.8rem">${formattedDate}</div>`)
document.write(`<div style="color: black; font-weight: bold;font-size: 0.8rem;">${this.text}</div>`)
document.write(`<div style="color: black; font-weight: bold;font-size: 1rem">${this.arrayOfTags}</div>`)
}
}
news = new News("Заголовок",
"В ближайшие дни в Украине будет дождливая погода, в понедельник в Карпатах прогнозируют мокрый снег.",
"#teg1, #teg2, #teg3", "2025-05-14");
news.print();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment