Nikito4ka_ka
@Nikito4ka_ka

Как отобразить только время используя class Date?

Здравствуйте, получая с openweather API дату и время

{weather.sys ? (<p> {Date((weather.sys.sunrise + weather.timezone) * 1000)}</p>) : null}


Выводится полностью дата и время
635c0cb7f2121121267045.png

Пытался использовать moment но почему-то при попытке вывода время отличается.

Подскажите как используя class Date в React.js получить только часы и минуты (HH:mm)?

Мой код

import React, { Fragment } from 'react';
import { Navbar } from '../../Components/Navbar';
import axios from 'axios';
import moment from 'moment';

const API_endpoint = `https://api.openweathermap.org/data/2.5/weather?`;
const API_key = `50a38429d7746f9b127b549eef8088c3`;

export const Home = () => {
	const [details, setDetails] = React.useState(null);
	const [weather, setWeather] = React.useState([]);
	const getUserGeolocationDetails = () => {
		if (details === null) {
			fetch(
				'https://geolocation-db.com/json/6ba8ff30-456e-11ed-b55d-a515c0aa6157'
			)
				.then(response => response.json())
				.then(data => setDetails(data));

			// console.log(details);
		}
	};

	const getWeatherDetails = () => {
		let endWeather = `${API_endpoint}q=${details.city},${details.country_code}&units=metric&appid=${API_key}`;
		axios.get(endWeather).then(response => {
			setWeather(response.data);
		});
	};

	React.useEffect(() => {
		getUserGeolocationDetails();

		if (details !== null) {
			getWeatherDetails();
		}
	});

	return (
		<Fragment>
			<Navbar />
			<div className='home-wrap'>
				<div className='widget'>
					<div className='widget-item time'>
					       {weather.sys ? (
							<p>{Date((weather.sys.sunrise + weather.timezone) * 1000)}</p>
						) : null}
					</div>
				</div>
			</div>
		</Fragment>
	);
};

  • Вопрос задан
  • 55 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы