计算在给定年份内13日下降的星期五的数量

时间:2015-11-04 05:55:04

标签: javascript loops

我想创建一个贯穿给定年份的月份的函数,计算13日的星期五,并返回该数字。到目前为止,这就是我所拥有的:

function numberOfFridaythe13thsIn(jahr){
    var d = new Date();
    d.setFullYear(jahr, 0, 13);
    var counter = 0;
    var months = 0;
    while(months <= 11) {
        months++;
        if(d.getDay() == 5 && d.getDate() == 13) {
          counter++;
       }
    }
    return counter;                            
}

我想象这个代码从某一年的1月13日开始,有一个计数器,其中的天数总和将会循环,并且将在几个月内循环。我知道我的代码已关闭,但我可以得到一些指导吗?

3 个答案:

答案 0 :(得分:4)

试试这个:

function numberOfFridaythe13thsIn(jahr){
    var d = new Date();
    var counter = 0;
    var month;

    for(month=0;month<12;month++)
    {
     d.setFullYear(jahr, month,13);
        if (d.getDay() == 5)
        {
          counter++;
        }
    }

    return counter;                            
}

基本上,一年只有十二天,日期为13.所以,我们只需循环浏览每一天,检查是否是星期五。

答案 1 :(得分:3)

您缺少的重要一点是在每次循环迭代时更新日期。

function numberOfFridaythe13thsIn(jahr) {
    var count = 0;
    for (var month=0; month<12; month++) {
        var d = new Date(jahr,month,13);
        if(d.getDay() == 5){
          count++;
       }
    }
    return count;                            
}

console.log(numberOfFridaythe13thsIn(2015));

答案 2 :(得分:0)

我认为它会对你有帮助......

package tryupload;

import java.io.File;
import java.io.FileInputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletContext;

import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

import sun.misc.ExtensionInstallationProvider;

public class NextFileParser extends ActionSupport implements ServletContextAware{
    /**
     * 
     */
    private static final long serialVersionUID = 1678420053045158846L;
    POIFSFileSystem fs = null;
    ParseDocBean pdb=new ParseDocBean();
    //private File[] fileList;
    static int i=0;
    private ServletContext servletContext;
    private ArrayList<ParseDocBean> list=new ArrayList<ParseDocBean>();
    public ServletContext getServletContext() {
        return servletContext;
    }

@Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

public String execute(){

    String fileServerpath=getServletContext().getRealPath("/doc/");
    System.out.println(fileServerpath);
                File f=new File(fileServerpath);
                File[] docs=f.listFiles();
                //System.out.println(docs.length);
                if(parseNext(docs))
                    System.out.println("parsed");
                System.out.println("inside execute method");
                //NextFileParser nfp=new NextFileParser();
                //nfp.setFileList(docs);
                //nfp.setFileList(docs);
                //NextFileParser.parseNext(docs);

                i++;        
            return SUCCESS;

}


    public boolean parseNext(File[] fileList){
        System.out.println(fileList.length);
        System.out.println(i);
        String Content=null;
        try{
        if(i==fileList.length){
            //msg="Finished";
            pdb.setMsg("Finished");
            list.add(pdb);

            return false;
        }
        else{
            String fname=fileList[i].getName();
            System.out.println(fname);
            String basename=FilenameUtils.getBaseName(fname);
            //String extension=FilenameUtils.getExtension(fname);
                    if(checkAlreadyExists(basename))
                    {
                        //addFieldError("content", "Already Parsed");
                        //str="No new Files";
                        pdb.setStr("No new Files");
                        list.add(pdb);
                        return false;
                        }

                    else{

                                try
                                {
                                        fs = new POIFSFileSystem(new FileInputStream(fileList[i]));
                                        HWPFDocument doc = new HWPFDocument(fs);
                                        WordExtractor we = new WordExtractor(doc);
                                       // content= we.getText();//if the extension is .doc
                                         ParseDocBean pdb=new ParseDocBean();
                                        System.out.println(we.getText());
                                       pdb.setContent(we.getText());
                                       list.add(pdb);
                                        /*pdb.setFinishMsg("Finished");*/
                                        we.close();

                                        System.out.println(i);

                                }
                                catch (Exception e)
                                {
                                    e.getStackTrace();
                                    e.getMessage();
                                    System.out.println("document file cant be parsed");
                                }
                    }
        }
        }

                    catch (Exception e) {
                        e.getMessage();
                    }
        return true;


    /*  if(!Content.equals("")){
        System.out.println(Content);}
        return Content;
        */
    }


    public static boolean checkAlreadyExists(String basename){
        boolean status=false;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/resume","root","root");
            if(con!=null){
                System.out.println("Connection ok");
            }

            String query="{call checkFileAlreadyExists(?,?)}";
            CallableStatement st;
            st=con.prepareCall(query);
            st.setString(1, basename);
            st.registerOutParameter(2, java.sql.Types.INTEGER);
            st.execute();
            int id=st.getInt(2);
            if(id!=0){
                status=true;
            }
            else
            {
                status=false;
            }
            con.close();
            st.close();
        }
        catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

if(status){
            System.out.println("All ready parsed");
            return true;
            }
else
        {
            return false;
                    }


    }


}

适用于MM / dd / yyyy格式。