在javascript

时间:2017-11-17 20:41:45

标签: javascript csv export-to-csv downloading save-as

我仍然是javascript的初学者,仍在训练中,我制作了一个计算日出,日落,中午和日程的代码。午夜时分。

我可以通过StackOverflow中的许多主题帮助解决所有问题(即使它不是专业问题)。

我可以使用document.write()console.log()来使用我的结果。

我知道,如何将结果保存为csv文件,而不是使用document.write()显示结果

我显示的结果已经是csv格式:

  

01/01 / yyyy,06:00:00,12:00:00,18:00:00,00:00:00 ......   一十二分之三十一月/年,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

1 个答案:

答案 0 :(得分:0)

包括这个方便的&#34; saveAs&#34;功能在文档的某个地方。

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