[英]convert totaltime every time user login javascript
我希望每次用户登录时都将totalTime设置为可读格式,但是这样我只能获得id(1)的时间,其余时间以毫秒为单位显示。我如何使用JavaScript + jsp + springmvc + hibernate做到这一点
<%@page import=" com.ephesoft.timesheet.core.model.LoginLog"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function convertTime(milliSeconds) {
var str, days, hours, minutes, seconds;
if (milliSeconds > 0) {
milliseconds = milliSeconds;
days = Math.floor(milliseconds / (24 * 60 * 60 * 1000));
if (days < 0) {
days = 0;
}
milliseconds -= days * 24 * 60 * 60 * 1000;
hours = Math.floor(milliseconds / (60 * 60 * 1000));
if (hours < 0) {
hours = 0;
}
milliseconds -= hours * 60 * 60 * 1000;
minutes = Math.floor(milliseconds / (60 * 1000));
if (minutes < 0) {
minutes = 0;
}
milliseconds -= minutes * 60 * 1000;
seconds = Math.floor(milliseconds / (1000));
if (seconds < 0) {
seconds = 0;
}
} else {
days = hours = minutes = seconds = 0;
}
str = days + ":" + hours + ":" + minutes + ":" + seconds;
return str;
};
</script>
</head>
<table border="1">
<thead>
<tr>
<td>Login Id</td>
<td>Login Time</td>
<td>Logout Time</td>
<td>Total Time(in Minutes)</td>
</tr>
</thead>
<--- changes for Date --->
<%
List<LoginLog> log = (List<LoginLog>) r equest.getAttribute("loginList");
if(log != null) {
for(LoginLog login : log) {
String format = new SimpleDateFormat("EEE, d MMM yyyy ", Locale.ENGLISH).format(login.getLoginTime());
%>
<--- --->
<tr>
<td><%=login.getId()%></td>
<td><%=login.getLoginTime()%></td>
<td><%=login.getLogoutTime()%></td>
<td id="a"><span id="time"><%=login.getTotalTime()%></span> <script>
var milliSeconds = document.getElementById("time").innerHTML;
var c = convertTime(milliSeconds);
document.getElementById("a").innerHTML = c;
</script></td>
</tr>
<%
}
}
%>
</table>
</form>
</body>
</html>
can anyone please tell me appropriate way? THANKS.
您的问题在于这段代码
<table border="1">
<thead>
<tr>
<td>Login Id</td>
<td>Login Time</td>
<td>Logout Time</td>
<td>Total Time(in Minutes)</td>
</tr>
</thead>
<%
List<LoginLog> log = (List<LoginLog>)request.getAttribute("loginList");
if(log != null) {
for(LoginLog login : log) {
%>
<tr>
<td><%=login.getId()%></td>
<td><%=login.getLoginTime()%></td>
<td><%=login.getLogoutTime()%></td>
<!-- changes -->
<td id="a<%=login.getId()%>"><span id="time<%=login.getId()%>"><%=login.getTotalTime()%></span> <script>
var milliSeconds = document.getElementById("time<%=login.getId()%>").innerHTML;
var c = convertTime(milliSeconds);
document.getElementById("a<%=login.getId()%>").innerHTML = c;
</script></td>
</tr>
<%
}
}
%>
</table>
您正在创建几行,并且所有行都有相同的ID “时间”,然后当您在JS中说到
getElementById("id")
会找到第一个出现的地方,这就是为什么您总是有相同的日期。
要使其正常工作,您应该使ID唯一,这是一个选项:
<span id="time<%login.getId()%>">
然后在您的JS代码中
var milliSeconds = document.getElementById("time"<%=login.getId()%>).innerHTML;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.