LoginSignup
0
0

More than 5 years have passed since last update.

Ae openweathermapAPIのUNIX時間をローカル時間に変換

Last updated at Posted at 2018-05-30

コンストラクタは本当に便利

前回に引き続きopenweathermapAPIの作業の続きですが、今日は日の出と日の入りの時間を取得しようと思います。
OpenWeatherMapAPIにはこのようにjsonが返ってきます。


{
"coord": {
"lon": 127.8,
"lat": 26.34
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 28.66,
"pressure": 1013,
"humidity": 79,
"temp_min": 28,
"temp_max": 29
},
"visibility": 16093,
"wind": {
"speed": 4.6,
"deg": 210
},
"clouds": {
"all": 1
},
"dt": 1527638400,
"sys": {
"type": 1,
"id": 7625,
"message": 0.0061,
"country": "JP",
"sunrise": 1527626201,
"sunset": 1527675366
},
"id": 1894616,
"name": "Okinawa",
"cod": 200
}

この中に
"sunrise": 1527626201,
"sunset": 1527675366
というものがあるのでこれを取得するのですが、この数値がUNIXTIMEになります。

date.sunriseで1527626201
date.sunsetで1527675366
が返ってくるイメージです。

これをUNIXTIMEからローカル時間に変換します。
※今回は動作確認の為jsonからではなく直接数値を入力しています。

UnixTime_convert_LocalTime(1527626201);
UnixTime_convert_LocalTime(1527675366);

の部分です。

実際にjsonを走らせた時は

UnixTime_convert_LocalTime(date.sunrise);
UnixTime_convert_LocalTime(date.sunset);

のような形になります。

今日のソースはこれ

var UconvertL = [];
function UnixTime_convert_LocalTime(data){
    UconvertL = [];//空にする
    var d = new Date( data* 1000 );
    var year  = d.getFullYear();
    var month = d.getMonth() + 1;
    var day  = d.getDate();
    var dayOfWeek = d.getDay() ;
    var hour = ( d.getHours()   < 10 ) ? '0' + d.getHours()   : d.getHours();
    var min  = ( d.getMinutes() < 10 ) ? '0' + d.getMinutes() : d.getMinutes();
    var sec   = ( d.getSeconds() < 10 ) ? '0' + d.getSeconds() : d.getSeconds();
    var dayOfWeekEnStr = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ][dayOfWeek] ;
    var monthEnStr = ["January","February","March","April","May","June","July","August","September","October","November","December"][month];
    var dayOfWeekJpStr = [ "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日" ][dayOfWeek] ;
    UconvertL.push(year,month,day,hour,min,sec,monthEnStr,dayOfWeekEnStr,dayOfWeekJpStr);
    $.writeln( "=========================================" );
    //$.writeln( year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec +monthEnStr+" "+dayOfWeekEnStr+" "+dayOfWeekJpStr);

}

 UnixTime_convert_LocalTime(1527626201);
$.writeln( "日の出" );
$.writeln( UconvertL[0] + '-' + UconvertL[1] + '-' + UconvertL[2] + ' ' + UconvertL[3] + ':' + UconvertL[4] + ':' + UconvertL[5]+UconvertL[6]+" "+UconvertL[7]+" "+UconvertL[8]);
        for (var i = 0 ; i<UconvertL.length; i ++){
        $.writeln("UconvertLの配列"+"["+[i]+"]"+"="+UconvertL[i]);
        }
 $.writeln( "日の入" );
 UnixTime_convert_LocalTime(1527675366);
  $.writeln( "日の出" );
  $.writeln( UconvertL[0] + '-' + UconvertL[1] + '-' + UconvertL[2] + ' ' + UconvertL[3] + ':' + UconvertL[4] + ':' + UconvertL[5]+UconvertL[6]+" "+UconvertL[7]+" "+UconvertL[8]);
        for (var i = 0 ; i<UconvertL.length; i ++){
        $.writeln("UconvertLの配列"+"["+[i]+"]"+"="+UconvertL[i]);
        }

コンソールの出力結果が

=========================================
日の出
2018-5-30 05:36:41June Wednesday 水曜日
UconvertLの配列[0]=2018
UconvertLの配列[1]=5
UconvertLの配列[2]=30
UconvertLの配列[3]=05
UconvertLの配列[4]=36
UconvertLの配列[5]=41
UconvertLの配列[6]=June
UconvertLの配列[7]=Wednesday
UconvertLの配列[8]=水曜日
日の入
=========================================
日の出
2018-5-30 19:16:06June Wednesday 水曜日
UconvertLの配列[0]=2018
UconvertLの配列[1]=5
UconvertLの配列[2]=30
UconvertLの配列[3]=19
UconvertLの配列[4]=16
UconvertLの配列[5]=06
UconvertLの配列[6]=June
UconvertLの配列[7]=Wednesday
UconvertLの配列[8]=水曜日
結果 : undefined

となります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0