简体   繁体   English

如何获得具体日期?

[英]How to get specific date?

I have to show date on my jsp that date will be the up coming wednesday date of week.For example if today is "friday 3 june 2012" and customer get login today then i have to show him mgs that Your order will be deliver on Wednesday "date". 我必须在我的jsp上显示日期,该日期将是一周中即将到来的星期三。例如,如果今天是“ 2012年6月3日,星期五”并且客户今天登录,则我必须向他显示您的订单将被交付星期三“日期”。 that "date" will be the live date of coming wednesday meanse Wednesday 8 june 2012. how i get my requirement using Jquery . 该“日期”将是即将到来的星期三的实况日期,这意味着2012年6月8日,星期三。我如何使用Jquery获得要求。

With this you are able to get the weekday of today: 有了这个,您可以得到今天的工作日:

var d = new Date();
var n = d.getDay(); 

A 3 means Wednesday so create a loop: 3表示星期三,因此请创建一个循环:

var d = new Date();
while (d.getDay() != 3) {
    d = new Date(d.getFullYear(), d.getMonth(), d.getDate()+1);
}

Update 更新

Working solution: http://jsfiddle.net/mavrick3/3UEAZ/1/ 工作解决方案: http : //jsfiddle.net/mavrick3/3UEAZ/1/

you have to do calculation on current date 您必须在当前日期进行计算

var currentTime = new Date()

just find out the days to add in current date and do processing according your need. 只需找出要添加当前日期的日期,然后根据需要进行处理即可。

  <h4>It is now  
   <script type="text/javascript">
    <!--
     var currentTime = new Date()
      var hours = currentTime.getHours()
         var minutes = currentTime.getMinutes()
           if (minutes < 10){
           minutes = "0" + minutes
                  }
             document.write(hours + ":" + minutes + " ")
           f(hours > 11){
               document.write("PM")
                 } else {
           document.write("AM")
            }
               //-->
            </script>
               </h4>

Macci Refer this Macci请参考

You could use something like the following: 您可以使用如下所示的内容:

var DateCalc = (function () {
    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
        months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
        getNextWed = function () {
            var d = new Date();
            while (d.getDay() != 3) {
                d.setDate(d.getDate() + 1);
            }
            return d;
        },
        formatDate = function (d) {
            var str = [];

            str.push(days[d.getDay(days)]);
            str.push(d.getDate());    
            str.push(months[d.getMonth()]);
            str.push(d.getFullYear());

            return str.join(' ');
        };           
    return {
        getNextWedStr: function () {
            return formatDate(getNextWed());
        }
    };
})();

console.log(DateCalc.getNextWedStr()); // "Wednesday 27 June 2012 "

Here's a working example . 这是一个有效的例子

This will match the current day if it's a Wednesday. 如果是星期三,则将与当前日期匹配。 If it always has to be the next Wednesday then just do: 如果始终必须在下一个星期三进行操作,请执行以下操作:

...
var d = new Date();
d.setDate(d.getDate() + 1); // <-- this
while (d.getDay() != 3) {
...

Using Java in JSP page, you can do it in below way. 在JSP页面中使用Java ,您可以通过以下方式进行。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
*
* @author fahim
*/
public class DateExample {

    public static void main(String[] args) {
        Calendar cal = new GregorianCalendar();

        System.out.println("" + cal.getTime());
        int i = cal.get(7);
        if (i == 1) { // sunday
            cal.add(Calendar.DAY_OF_WEEK, 3);
        } else if (i == 2) { // monday
            cal.add(Calendar.DAY_OF_WEEK, 2);
        } else if (i == 3) { // tuesdat
            cal.add(Calendar.DAY_OF_WEEK, 1);
        } else if (i == 4) { // wednesday
            cal.add(Calendar.DAY_OF_WEEK, 7);
        } else if (i == 5) { // hursday
            cal.add(Calendar.DAY_OF_WEEK, 6);
        } else if (i == 6) { // friday
            cal.add(Calendar.DAY_OF_WEEK, 5);
        } else if (i == 7) { // saturday
            cal.add(Calendar.DAY_OF_WEEK, 4);
        }        

        System.out.println("" + cal.getTime());
    }
}

Output I get as 我得到的输出

Wed Jun 27 15:39:44 AST 2012
Wed Jul 04 15:39:44 AST 2012

Demo at www.ideone.com 在www.ideone.com上进行演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM