JavaScript转换时间格式
/*将2010-1-6 19:8:2的时间格式转换成2010-01-06 19:08:02的时间格式*/
function getFormat(timeformat){
if(String(timeformat).length == 1){
timeformat = “0″ + String(timeformat);
}
return timeformat;
}
/*将2010-1-6 19:8:2的时间格式转换成2010-01-06 19:08:02的时间格式*/
function getFormat(timeformat){
if(String(timeformat).length == 1){
timeformat = “0″ + String(timeformat);
}
return timeformat;
}
function weblogin(){
var email = $(‘#reg_email’).val().replace(/\s/g,”");
var psw = $(‘#reg_psw’).val().replace(/\s/g,”");
var remember = $(“#remember”).attr(“checked”);
$.ajax(
{
type: “POST”,
url: ajaxBaseUrl + “/Public/doAjaxLogin”,
processData: false,
timeout: 20000,
error: function(){alert(“unknow error”);},
data: “login_email=” + email + “&login_psw=” + psw + “&remember=” + remember ,
success: function(msg)
{
if(msg.indexOf(“SUCCESS”)>=0){
document.getElementById(“btn_login”).disabled = “”;
}
}
}
);
}
<?php
if($_GET){
for($i=0; $i<10; $i++){
echo $_GET["id"];
}
exit;
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Ajax</title>
<script type=”text/javascript”>
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(url){
createXMLHttpRequest();
xmlHttp.open(“GET”, “?id=” + url, true);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
document.getElementById(“result”).innerHTML = xmlHttp.responseText;
}
}
xmlHttp.send(null);
}
</script>
</head>
<body>
<a href=”javascript:startRequest(‘o’);”>o</a>
<a href=”javascript:startRequest(‘f’);”>f</a>
<div id=”result”></div>
</body>
</html>
<script language=”javascript”>
<!–
function clock(){i=i-1
document.title=i+”秒后本内容将关闭!”;
if(i>0)setTimeout(“clock();”,1000);
else self.close();}
var i=120
clock();
//–>
</script>
timer_runting = setInterval(“CheckMsgs();”,30000);
function CheckMsgs(){
……
}
setTimeout(“document.getElementById(‘result’).style.display = ‘none’;”, 3600);
window.parent.document.getElementById(“defalut_loading_module”).style.display = “none”
function getFormat(timeformat){
if(String(timeformat).length == 1){
timeformat = “0″ + String(timeformat);
}
return timeformat;
}
var thetime = new Date();
document.write(getFormat(thetime.getFullYear()) + “-” + getFormat(thetime.getMonth()+1) + “-” + getFormat(thetime.getDate()) + ” ” + getFormat(thetime.getHours()) + “:” + getFormat(thetime.getMinutes()) + “:” + getFormat(thetime.getSeconds()));
<script type=”text/javascript”>
/////////////////////////////////////////////////
// 将2010-1-6 19:8:2的时间格式转换成2010-01-06 19:08:02的时间格式
// curdate:需要转换的时间
// ismonth:是否是月份,因为月份要加1
/////////////////////////////////////////////////
function getRealDate(curdate, ismonth){
if(ismonth){
curdate++;
}
if(String(curdate).length == 1){
curdate = “0″ + String(curdate);
}
return curdate;
}
</script>
<script language=”javascript” type=”text/javascript”>
/////////////////////////////////////////////////
// 将服务器时间转换成客户端时间(服务器用GMT时间)
// sourcetime:需要转换的时间,格式为2010-01-06 19:08:02
// nohis:是否需要时分秒,如果有则不需要
/////////////////////////////////////////////////
function toLocalTime(sourcetime, nohis){
var sourcetime = sourcetime.replace(/:/g, ‘-’);
var sourcetime = sourcetime.replace(/ /g, ‘-’);
var arr = sourcetime.split(“-”);//将时间格式化
var temptime = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3],arr[4],arr[5]));
var timestamp = temptime.getTime();//转换成时间戳
var temp = new Date(timestamp);
if(nohis){
var newtime = temp.getFullYear() + “-” + getRealDate(temp.getMonth(), 1) + “-” + getRealDate(temp.getDate());
}else{
var newtime = temp.getFullYear() + “-” + getRealDate(temp.getMonth(), 1) + “-” + getRealDate(temp.getDate()) + ” ” + getRealDate(temp.getHours()) + “:” + getRealDate(temp.getMinutes()) + “:” + getRealDate(temp.getSeconds());
}
return newtime;
}
var str = ’2010-01-06 19:08:02′;
document.write(“服务器端时间:” + str + “<br />客户端时间:” + toLocalTime(str));
</script>