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

task 1 is implemented #5

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
67 changes: 44 additions & 23 deletions Task 1/checkDate.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
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();

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();

let isSameDate = false;

if (year == currentYear) {
if (month == current_month) {
if (day == current_day) {
isSameDate = true;
} else {
isSameDate = false;
}
}
//Сделал ограничение на тип параметра timestamp

if (typeof timestamp !== "number") {
throw new Error("expected number");
}

// Заменил ключевое слово var на const, потому что var - устаревший способ обявления переменных,
// и значения переменных не будут изменяться

// Использовал вместо многочисленных вызовов методов для получения отдельных компонентов даты
// метод toLocaleDateString с соответствующими опциями
// Это решение вносит большей читаемости кода

const LOCALE_CODE = "en-US";

const DATE_OPTIONS = {
day: "numeric",
month: "numeric",
year: "numeric",
};

const date = new Date(timestamp * 1000);
const hours = date.getHours();
const localeDate = date.toLocaleDateString(LOCALE_CODE, DATE_OPTIONS);

//Избавился от конструкции new Date(Date.now()), так как такая конструкция бессмысленна

const currentDate = new Date();
const currentLocaleDate = currentDate.toLocaleDateString(
LOCALE_CODE,
DATE_OPTIONS
);

//Избавился от конструкции if для большей читаемости логического выражения

const isSameDate = localeDate === currentLocaleDate;

//pm начинается с 12 часов

const dayPeriod = hours >= 12 ? "pm" : "am";

//Вынес логику за пределы return

return {
isSameDate: isSameDate,
dayPeriod: hour > 11 ? 'pm' : 'am'
}
isSameDate,
dayPeriod,
};
}