前提・実現したいこと
JavaScriptでUTCからJSTに変換したい
発生している問題・エラーメッセージ
後述するソースコードの二つ目のif文がなぜか通ってしまうため正しい値が出力されないです
該当のソースコード
Js
1 2<!DOCTYPE html> 3<html lang="ja"> 4 <meta charset="utf-8" /> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 6 <meta name="viewport" content="width=device-width, initial-scale=1" /> 7 <head> 8 <title>最終課題</title> 9 <link 10 href="./bootstrap-4.4.1-dist/css/bootstrap.min.css" 11 rel="stylesheet" 12 /> 13 <script src="./jquery-3.4.1.min.js"></script> 14 <script src="./bootstrap-4.4.1-dist/js/bootstrap.min.js"></script> 15 <script> 16 $(function() { 17 const url = 18 "http://api.openweathermap.org/data/2.5/forecast?id=1849814&APPID="; 19 const key = "a362e731b66c5036016466ff86f756e4"; 20 21 var position; 22 $.getJSON(url + key, function(json) { 23 $("#place").append("<h2>" + json.city.name + "</h3>"); 24 for (var i = 0; i < json.list.length; i++) { 25 var tenki = ""; 26 var date = json.list[i].dt_txt; 27 28 var year = Number(date.substr(0, 4)); 29 // console.log(date); 30 // console.log(year); 31 var month = Number(date.substr(5, 2)); 32 // console.log(month); 33 var day = Number(date.substr(8, 2)); 34 // console.log(day); 35 var hour = Number(date.substr(11, 2)); 36 // console.log(hour); 37 // 2020-01-18 21:00:00 38 39 hour += 9; 40 41 if (hour > 24) { 42 hour -= 24; 43 day += 1; 44 if ( 45 (day > 31 && month == 1, 3, 5, 7, 8, 10, 12) || 46 (day == 30 && month == 4, 6, 9, 11) || 47 (day > 28 && month == 2) || 48 (day > 29 && month == 2 && year % 4 == 0) 49 ) { 50 day -= 12; 51 month += 1; 52 console.log( 53 String(year) + 54 "年" + 55 String(month) + 56 "月" + 57 String(day) + 58 "日" + 59 String(hour) + 60 "時" 61 ); 62 if (month > 12) { 63 year += 1; 64 } 65 } 66 } 67 68 date = 69 String(year) + 70 "年" + 71 String(month) + 72 "月" + 73 String(day) + 74 "日" + 75 String(hour) + 76 "時"; 77 78 if (json.list[i].weather[0].main == "Clear") { 79 tenki = "はれ"; 80 } else if (json.list[i].weather[0].main == "Clouds") { 81 tenki = "くもり"; 82 } else if (json.list[i].weather[0].main == "Rain") { 83 tenki = "あめ"; 84 } 85 86 $(".container").append( 87 $(".table").append( 88 $(".tbody").append( 89 $('<tr id="data' + i + '"></tr>').append([ 90 $("<td></td>").text(date), 91 $("<td></td>").html( 92 ' <img src="' + 93 "http://openweathermap.org/img/w/" + 94 json.list[i].weather[0].icon + 95 '.png"> ' 96 ), 97 $("<td></td>").text(tenki) 98 ]) 99 ) 100 ) 101 ); 102 } 103 }); 104 }); 105 </script> 106 </head> 107 108 <body> 109 <h1>天気予報</h1> 110 <div> 111 <p id="place"></p> 112 </div> 113 114 <div class="container"> 115 <table class="table"> 116 <thead> 117 <tr> 118 <th>date</th> 119 <th>icon</th> 120 <th>weather</th> 121 </tr> 122 </thead> 123 <tbody class="tbody"> 124 <div class="data"></div> 125 </tbody> 126 </table> 127 </div> 128 </body> 129</html> 130
コードを省略せず全体を提示してください。
修正しました。
回答1件
あなたの回答
tips
プレビュー