2017-11-17 15 views
-1

まだ初心者ですが、日曜、日の入り、正午&深夜の時刻を計算するコードを作成します。JavaScriptでCSVファイルに保存

StackOverflowの多くのトピックの助けを借りて、すべての問題が(プロではなくても)問題なしにすることができました。

私の結果を使用するにはdocument.write()またはconsole.log()を使用できます。

私は、csvファイルとしての私の結果を保存する代わりに document.write()

マイ表示された結果と私の結果を表示する方法をcsv形式ですでに知っているだろう

01/01/YYYY、06: 00:00,12:00:00,18:00:00:00:00 ...... 31/12/yyyy、06:00:00,12:00:00,18:00: 00,00:00:00

結論:私の結果をcsvファイルとして保存する方法を説明してください。

HTML:

<!DOCTYPE html> <html> 
    <head> 
     <title>Sunrise and Sunset</title> 
    </head> 
    <body> 
    <form> 
    Latitude: <input id='lat' value='52.0'> 
    <br> 
    Longitude: <input id='lon' value='10.0'> 
    <br> 
    Elevation: <input id='elv' value='0.0'> 
    <br><br> 
    Year: <input id='yr' value='2000'> 
    <br> 
    Time zone: <input id='tz' value='1.0'> 
    <br> 
    <button id='btn' onclick='rechner();'> 
    Get your data 
    </button> 
    </body> </html> 

Javascriptを:任意の助け

//-------Degrees to radians------// 
function toRad(angle){ 

    return angle*Math.PI/180; 
}; 

//------Radians to degrees-------// 
function toDeg(angle){ 

    return angle*180/Math.PI; 
}; 

//---------Main function-------// 

function rechner(){ 
    var lat,lon,elv,year,tzone; 

lat = parseFloat(document.getElementById('lat').value); 
lon = parseFloat(document.getElementById('lon').value); 
elv = parseFloat(document.getElementById('elv').value); 

year = parseInt(document.getElementById('yr').value); 
tzone = parseFloat(document.getElementById('tz').value); 

    var dip_angle = 0.0347*Math.sqrt(elv); 
//dip angle for sunrise and sunset accounting for elevation of the location, it is here in degrees expressed 

    var date = new Date(year,0,1,12); 
    var time = date.getTime(); 
    var julian_date = (time/86400000) - (date.getTimezoneOffset()/1440) + 2440587.5; 
    var d = julian_date - 2451545.0; 

var yr = date.getFullYear(); 
// get year from the date 01/01/YYYY 


    for(var i=0; i<=((yr%4)==0 ? 365 : 364); i++){ 

    var D = d+i; 

//------Solar approximate coordinates----// 

    var g = ((357.529 + 0.98560028*D)+360)%360; 
//Mean anomaly of the Sun 

    var q = ((280.459 + 0.98564736*D)+360)%360; 
//Mean longitude of the Sun 

    var L = ((q + 1.915*Math.sin(toRad(g)) + 0.020*Math.sin(toRad(2*g)))+360)%360; 
//Geocentric apparent ecliptic longitude of the Sun (adjusted for aberration) 

    var e = 23.439 - 0.00000036*D; 
//Mean obliquity of the ecliptic 

    var ra = ((toDeg(Math.atan2(Math.cos(toRad(e))*Math.sin(toRad(L)),Math.cos(toRad(L)))))+360)%360; 
// Right ascension in deg 

    var dec = toDeg(Math.asin(Math.sin(toRad(e))*Math.sin(toRad(L)))); 
// Declination in deg 

    var eqt = (((q - ra)/15)+24)%24 
//equation of time, in hrs 

    var R = 1.00014 - 0.01671*Math.cos(toRad(g)) - 0.00014*Math.cos(toRad(2*g)); 
// Distance Sun-Earth in AU 

    var SD = 0.2666/R; 
// Semi-diameter of sun in degrees 

//---function calculates timestamp from noon to the wanted angle under horizon--// 

function timeTodip (angle){ 
    return 1/15*toDeg(Math.acos((-Math.sin(toRad(angle))-Math.sin(toRad (lat))*Math.sin(toRad(dec)))/(Math.cos(toRad(lat))*Math.cos(toRad(dec))))); 
}; 

//function renders hh.hhh in range [0,24] 
    function rendHr(y){ 
if(y>=0 && y<=24){return y;} 
else if(y>24){return y%24;} 
else{return y%24 + 24} 
    }; 

//---function converts hh.hhhh to hh:mm:ss 
    function hhToHMS(nbr){ 
var decimalTimeString = nbr; 
var n = new Date(0,0); 
n.setSeconds(+decimalTimeString * 60 * 60); 
return n.toTimeString().slice(0, 8) 
}; 

//---function gets date as DD/MM/YYYY---// 

function convertDate(inputFormat) { 
    function pad(s) { return (s < 10) ? '0' + s : s; } 
    var z = new Date(inputFormat); 
    return [pad(z.getDate()), pad(z.getMonth()+1), z.getFullYear()].join('/'); 
}; 

var noon = rendHr(12 + tzone - lon/15 - eqt); //noon time in format hh.hhhh 


//--------Sunrise and sunset calc--------// 

var sunrise = noon - timeTodip(0.833+dip_angle); //sunrise 
var sunset = noon + timeTodip(0.833+dip_angle); //sunset 

var mid = (24-rendHr(sunset-sunrise))/2 + sunset; //solar midnight 

var nbrDay= new Date((10957+(julian_date - 2451545.0)+i)*86400000); 

document.write(convertDate(nbrDay)+','+hhToHMS(sunrise)+','+hhToHMS(noon)+','+hhToHMS(sunset)+','+hhToHMS(mid)+'<br>'); 

    }; 
}; 

おかげ

編集Jsfiddle

js linter says it's good

+0

クライアントにダウンロード可能なファイルとしてそれを提示する方法を、あなたは意味 –

+0

')('のdocument.writeを使用することはありませんか?それをサーバーに保存する方法について説明します。クライアントは例えば –

+0

をクリックする必要があります。ボタンを押すと、forループで生成されたデータのリスト全体を保存するまでsaveAs(text、filename)関数が関数内になければ、ブラウザ – Khaled

答えて

0

ドキュメントのどこかに便利な「saveAs」関数が含まれています。

その後、
function saveAs(text, filename){ 
    var pom = document.createElement('a'); 
    pom.setAttribute('href', 'data:text/plain;charset=urf-8,'+encodeURIComponent(text)); 
    pom.setAttribute('download', filename); 
    pom.click(); 
}; 

そして、それを使用します。

手始めに
saveAs(myCSVString, "data.csv"); 
+0

に表示されずにcsvとしてダウンロードされます。 myCSVStringではなくdocument.write()の間に直接何を書いていますか? – Khaled

+0

あなたがその機能を置く場所はあなた次第です。あなたの現在の 'rechner'関数内でスコープを適用したい場合は、そうすることができます。そして、グローバルな範囲を汚染することに懸念があるなら、それが望ましいでしょう。しかし、あなたのアプリケーションの別の部分でこの関数を再利用したい場合、 'rechner'関数の外に置くとグローバルにアクセスできるようにする方が望ましいかもしれません。 –

+0

https://jsfiddle.net/8mkpoc31/1/どこにエラーがありますか? : – Khaled

関連する問題