使用getDay()显示第二天

时间:2015-09-19 10:56:28

标签: javascript

我需要一种方法来使用getDay();

显示一周的第二天

我没有考虑过多:

var d = new Date();
var weekday = d.getDay() +1;

然后我意识到除了周六之外的大部分时间都会有效。由于星期六是第六天,如果我加一个,它将返回7而不是0,即星期日。

我该怎么做?

3 个答案:

答案 0 :(得分:4)

var weekday = (d.getDay() + 1) % 7

模运算符返回除法的余数7,因此如果结果为7(6 + 1),则7 % 7 === 0的模数

答案 1 :(得分:2)

另一种选择:

class Populate extends AsyncTask<String, String, JSONArray> {

        protected void onPreExecute() {
            progress = ProgressDialog.show(RecipeSearch.this, "Finding Recipes", "Searching....", true);
        }

        @Override
        protected JSONArray doInBackground(String... urls) {
            JSONParser recipeParse = new JSONParser();
            String rawJSON = recipeParse.getJSON(urls[0]);

            try {

                if (rawJSON != null) {
                    JSONObject object = new JSONObject(rawJSON);
                    JSONArray jArray = object.getJSONArray("recipes");
                    return jArray;
                } else {
                    JSONArray jArray = null;
                    return jArray;
                }

            } catch(Exception e) {
                System.out.println(e);
                Toast.makeText(getApplicationContext(), "Error Searching for Recipes",
                        Toast.LENGTH_SHORT).show();

             JSONArray jArray = null;
                return jArray;
            }
            //do http request and add objects to array here.
            //need to do a custom adapter
           // return null;
        }

        @Override
        protected void onPostExecute(JSONArray jArray) {

            if (jArray != null) {
                final ListView recipes = (ListView) findViewById(R.id.recipeView);

                recipes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        JSONObject selected = (JSONObject) (recipes.getItemAtPosition(position));

                        String url = null;
                        try {
                            url = selected.getString("source_url");
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                        }
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);

                    }
                });

                recipeAdapter = new RecipeAdapter(RecipeSearch.this, jArray);//jArray is your json array
                recipes.setAdapter(recipeAdapter);

            } else {
                Toast.makeText(getApplicationContext(), "No Recipes Found", Toast.LENGTH_SHORT).show();
                finish();
            }
            progress.dismiss();
        }
    }

...因为JavaScript public class RecipeAdapter extends BaseAdapter implements ListAdapter { private final Activity activity; private final JSONArray jsonArray; public RecipeAdapter (Activity activity, JSONArray jsonArray){ assert activity !=null; assert jsonArray != null; this.jsonArray = jsonArray; this.activity = activity; } @Override public int getCount(){ if(null==jsonArray) return 0; else return jsonArray.length(); } @Override public JSONObject getItem(int position){ if(null==jsonArray) return null; else return jsonArray.optJSONObject(position); } @Override public long getItemId(int position){ return 0; } @Override public View getView (int position, View v, ViewGroup parent){ if (v == null) { v = View.inflate(activity, R.layout.recipe_item, null); } CircularImageView icon = (CircularImageView)v.findViewById(R.id.recipeIcon); TextView title = (TextView) v.findViewById(R.id.recipeTitle); TextView supplier = (TextView) v.findViewById(R.id.supplier); JSONObject JSdata = getItem(position); if(null!=JSdata){ try { if (JSdata.has("title")) { title.setText(JSdata.getString("title")); } if (JSdata.has("publisher")) { supplier.setText(JSdata.getString("publisher")); } if (JSdata.has("image_url")){ String image = JSdata.getString("image_url"); if (image != null) { Picasso.with(activity) .load(image) .centerCrop() .resize(50, 50) .into(icon); } } else { Toast.makeText(activity, "Image not loaded", Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } ImageView fav = (ImageView)v.findViewById(R.id.favbutton); fav.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(activity, "Fav", Toast.LENGTH_SHORT).show(); } }); return v; } } 对翻转很聪明。

答案 2 :(得分:1)

var weekday = d.getDay() === 6 ? 0 : d.getDay()+1;