Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: task 1 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions Task 1/checkDate.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
function checkDate(timestamp) {
var day = new Date(timestamp * 1000).getDate();
var month = new Date(timestamp * 1000).getMonth();
var year = new Date(timestamp * 1000).getFullYear();
var hour = new Date(timestamp * 1000).getHours();
//Для наглядности, все названия переменных будут написаны в стиле camelCase

const current_Date = new Date(Date.now());
const current_day = current_Date.getDate();
const current_month = current_Date.getMonth() + 1;
const currentYear = current_Date.getFullYear();
//Const, потому что значения переменных ниже в функции не будут меняться, зависят только от параметра timestamp
const day = new Date(timestamp * 1000).getDate();
const month = new Date(timestamp * 1000).getMonth() + 1; //+1, т.к. без него присваивается предыдущий месяц
const year = new Date(timestamp * 1000).getFullYear();
const hour = new Date(timestamp * 1000).getHours();

const currentDate = new Date(Date.now());
const currentDay = currentDate.getDate();
const currentMonth = currentDate.getMonth() + 1;
const currentYear = currentDate.getFullYear();

let isSameDate = false;

if (year == currentYear) {
if (month == current_month) {
if (day == current_day) {
isSameDate = true;
} else {
isSameDate = false;
}
//для более точного сравнения, используeтся строгое равенство
//избавляемся от изначального присваивания false, т.к. переменной оно уже присвоено выше
if (year === currentYear) {
if (month === currentMonth) {
isSameDate = day === currentDay;
}
}

Expand All @@ -26,3 +27,6 @@ function checkDate(timestamp) {
dayPeriod: hour > 11 ? 'pm' : 'am'
}
}

console.log(checkDate(2345789)); //Wed, 28 Jan 1970 03:36:29 GMT, { isSameDate: false, dayPeriod: 'am' }
console.log(checkDate(1649359348)); //Thu, 07 Apr 2022 19:22:28 GMT, { isSameDate: true, dayPeriod: 'pm' }