在JSP 中写

 

    <%@ taglib uri ="http://java.sun.com/jsp.jstl/fmt"  prefix="fmt" % >

    <jsp:useBean id="currentTime" class="java.util.Date"/>

    <fmt:formatDate value="${currentTime}"/>

 

使用JavaScript 格式化当前日期

 

    <script type="text/javascript">
          var currDate = new Date();
          var str = currDate.getYear() + "-";
          str += currDate.getMonth() + 1 + "-";
          str +=currDate.getDate();
          document.write(str);
     </script>

 

today = new Date();

function time(){

today = new Date();

 var todayStr = today.getYear() + "-";

 if(today.getMonth()<10){

 todayStr=todayStr+"0"+(today.getMonth()+1);

 }

 else{

 todayStr=(todayStr+today.getMonth()+1);

 }

 if(today.getDate()<10){

 todayStr=todayStr+"-0"+today.getDate();

 }

 else{

 todayStr=todayStr+"-"+today.getDate();

 }

return todayStr;

};   
   

 


在Java 代码中格式化当前日期

 

DataFormat df = new SimpleDataFormat("yyyy-MM-dd");

df.format(new Date());