javascript中,求时间,
比如:
2009年2月,有28天,2009年3月有31天,
我想得到,2009年2月27日到2009年3月5日之间的日期,
结果应该是:
2009-2-27
2009-2-28
2009-3-01
2009-3-02
2009-3-03
2009-3-04
2009-3-05写一个函数,参数是前一个日期的YY和MM,后一个日期的YY和MM
最好把结果日期放在一个数组里返回,便于使用,先谢了,
比如:
2009年2月,有28天,2009年3月有31天,
我想得到,2009年2月27日到2009年3月5日之间的日期,
结果应该是:
2009-2-27
2009-2-28
2009-3-01
2009-3-02
2009-3-03
2009-3-04
2009-3-05写一个函数,参数是前一个日期的YY和MM,后一个日期的YY和MM
最好把结果日期放在一个数组里返回,便于使用,先谢了,
for( int i = 0 ; i++ ; i < count ){
第i天 = 开始时间 加上i天;
……
}
我试过了,根据
int count = 结束时间 - 开始时间;
这个出来是依照每个月31天计算的,假如那个月没有31天,就出问题了,,还要判断是平年还上润年,大月还是小月,这个很费时间,大伙有没有现成的代码,小弟谢了!
第i天 = 开始时间 加上i天;
……
}
public static void getDays( String startDate , String endDate ) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime( dateFormat.parse( startDate ) );
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime( dateFormat.parse( endDate ) );
long m = endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis();
int count = (int) m / 1000 / 60 / 60 / 24;
List< String > list = new ArrayList< String >();
for( int i = 0 ; i <= count + 1 ; i++ ){
startCalendar.add( Calendar.DATE , 1 );
String strDate = dateFormat.format( startCalendar.getTime() );
list.add( strDate );
}
for( int i = 0 ; i < list.size() ; i++ ){
System.out.println( list.get( i ) );
}
}
我就写下+1天的方法吧
JAVA---
/**
* 取得指定日期的下一天
*
* @param date
* 指定日期。
* @return 指定日期的下一天
*/ public static java.util.Date getNextDay(java.util.Date date) {
/**
* 详细设计: 1.指定日期加1天
*/ GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.add(Calendar.DATE, 1);
return gc.getTime();
}
/**
* �字符串转换Date
*
* @param str
* @return
*/
public static Date getStrToDate(String str, String format)throws Exception{
Date toDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
try {
toDate = formatter.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return toDate;
}JS---
function getNextDate(){
var nowdate =new Date('2009-01-31'.replace(/-/g, "/"));
var s =nowdate.getYear()+ "/"+(nowdate.getMonth() + 1) + "/"+(nowdate.getDate()+1);
nowdate=new Date(s);
alert(nowdate.toLocaleDateString());
}
<html>
<script>
function getDateArray(startDate, endDate) {
var year = startDate.replace(/(\d{2})[-]\d{1,2}[-]\d{1,2}/, "$1") - 0;
var month = startDate.replace(/\d{2}[-](\d{1,2})[-]\d{1,2}/, "$1") - 0;
var day = startDate.replace(/\d{2}[-]\d{1,2}[-](\d{1,2})/, "$1") - 0;
var _year = endDate.replace(/(\d{2})[-]\d{1,2}[-]\d{1,2}/, "$1") - 0;
var _month = endDate.replace(/\d{2}[-](\d{1,2})[-]\d{1,2}/, "$1") - 0;
var _day = endDate.replace(/\d{2}[-]\d{1,2}[-](\d{1,2})/, "$1") - 0;
var myDate = new Date(2000 + year, month, day, 0, 0, 0, 0);
var dateRange = new Object();
var count = 0;
var flag = false;
dateRange[String(count++)] = myDate;
while (!flag) {
myDate.setDate(myDate.getDate() + 1);
dateRange[String(count++)] = myDate.getYear() + "/" + myDate.getMonth() + "/" + myDate.getDate();
flag = ((myDate.getYear() - 2000 - _year == 0) && (myDate.getMonth() - _month == 0) && (myDate.getDate() - _day == 0));
if (myDate.getDate() == 9) break;
}
dateRange["length"] = count;
// 调用例子 开始
for (var i = 0; i < dateRange["length"]; i++) {
document.write(dateRange[String(i)] + "(" + i + ")<br>");
}
// 调用例子 结束
return dateRange;
}
</script>
<body onload="javascript:getDateArray('09-2-25', '09-3-5');">
</body>
</html>
<TITLE>test</TITLE>
</HEAD> <BODY>
<input type="button" onclick="diff('12-30-2008','01-12-2009')" value="test">
</BODY> <script>
function diff(startDate, endDate){
var sdate, edate , count;
sdate = new Date(startDate);
edate = new Date(endDate);
count = parseInt(Math.abs(sdate - edate) / 1000 / 60 / 60 /24);
alert(count);
}
</script>
</HTML>思路是一样。这是求间隔天数的代码,至于参数的格式,楼主做下字符串转换就好。
我就写下+1天的方法吧
JAVA---
/**
* 取得指定日期的下一天
*
* @param date
* 指定日期。
* @return 指定日期的下一天
*/ public static java.util.Date getNextDay(java.util.Date date) {
/**
* 详细设计: 1.指定日期加1天
*/ GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.add(Calendar.DATE, 1);
return gc.getTime();
} /**
* �字符串转换Date
*
* @param str
* @return
*/
public static Date getStrToDate(String str, String format)throws Exception{ Date toDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
try {
toDate = formatter.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return toDate;
} JavaScript---
function getNextDate(){
var nowdate =new Date('2009-01-31'.replace(/-/g, "/"));
var s =nowdate.getYear()+ "/"+(nowdate.getMonth() + 1) + "/"+(nowdate.getDate()+1);
nowdate=new Date(s);
alert(nowdate.toLocaleDateString());
}
function getNextDate(){
var nowdate =new Date('2009-02-28'.replace(/-/g, "/"));
var s =nowdate.getYear()+ "/"+(nowdate.getMonth() + 1) + "/"+(nowdate.getDate()+1);
nowdate=new Date(s);
alert(nowdate.toLocaleDateString());
}你试下嘛...>_< 2月28号的话+1天是不是3月1日
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>试试</title>
</head><script type="text/javascript">
/**
* 将 yyyy-MM-dd 格式转为日期
*/
function parseStr(s) {
s = s.replace(/-/g, '/');
return new Date(s);
}/**
* 在一个日期上增天天数
*/
function addDate(d, day) {
return new Date(d.getTime() + day * 24 * 3600 * 1000);
}/**
* 获得两个日期间(含)所有的日期
*/
function getAllDates(start, end) {
if(typeof start == 'string') {
start = parseStr(start);
}
if(typeof end == 'string') {
end = parseStr(end);
}
var diff = (end - start) / (24 * 3600 * 1000);
var dates = [];
for(var i = 0; i <= diff; i++) {
dates.push(addDate(start, i));
}
return dates;
}window.onload = function() {
var dates = getAllDates('2009-02-02', '2009-02-12');
for(var i = 0; i < dates.length; i++) {
alert(dates[i].toLocaleDateString());
}
}
</script><body></body>
</html>
<TITLE>test</TITLE>
</HEAD> <BODY>
<input type="button" onclick="getDays('2008-12-30','2009-1-12')" value="test">
</BODY> <script>
function getDays( startDate, endDate ){
var sdate, edate, count,fsDate, feDate;
var day = 24 * 60 * 60 * 1000;
fsDate = startDate.split( "-" );
feDate = endDate.split( "-" );
sdate = new Date( fsDate[1] + "-" + fsDate[2] + "-" + fsDate[0] );
edate = new Date( feDate[1] + "-" + feDate[2] + "-" + feDate[0] );
count = parseInt( Math.abs( sdate - edate ) / day );
for(var i = 0;i <=count ; i++ ){
var nextDay = new Date( sdate.getTime() + i*day );
var str = nextDay.getYear() + "-" + ( nextDay.getMonth() + 1 ) + "-" + nextDay.getDate();
alert( str );
}
}
</script>
</HTML>
但是没有考虑到,2009你2月只有28天,但是程序是按照31天来计算的这个不需要你考虑,javascript的Date.setDate()已经实现了这些功能了,就跟java.util.Calendar一样,它内部已经处理了,可以参考相关的apinew Date().setDate(32)会自动将时间调整为次月的第一日的不过肯思考,值得表扬,呵呵