473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Increment time in form

I have a form field which displays a time in the format 5:30 PM - this time
is selected from a previous page.
I need to add a function to increment or deincrement the time by clicking
up/down buttons - sort of like the clock in Windows.

How can this be done in javascript?

Thanks in advance!


Jul 23 '05 #1
2 5747

// disassambles to values
function processToValues() {
value = document.myform.time.value;
column = value.indexOf(":");
space = value.indexOf(" ");
half = value.substring(space+1);

// half would now countain PM or AM
hours = value.substring(0, column);
minutes = value.substring(column+1,space);
}

function increment() {
minutes++;
if (minutes>59) {
minutes=0;
hours++;
if (hours==12) {
// am->pm->am
// for now, case sensetive
half = (half=='PM'?'AM':'PM');
}
if (hours>12) {
hours = 1;
}
}
}

You should be able to figure out the rest. Good luck,
Vincent

Targa wrote:
I have a form field which displays a time in the format 5:30 PM - this time
is selected from a previous page.
I need to add a function to increment or deincrement the time by clicking
up/down buttons - sort of like the clock in Windows.

How can this be done in javascript?

Thanks in advance!


Jul 23 '05 #2
Targa wrote:
I have a form field which displays a time in the format 5:30 PM - this time
is selected from a previous page.
I need to add a function to increment or deincrement the time by clicking
up/down buttons - sort of like the clock in Windows.
How can this be done in javascript?
Thanks in advance!


Here's a quick stab at it:

<head>
<script type="text/javascript">

function getClockTime() {
//get current time
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var ap = "AM";
if (hour > 11) { ap = "PM"; }
if (hour > 12) { hour = hour - 12; }
if (hour == 0) { hour = 12; }
if (hour < 10) { hour = "0" + hour; }
if (minute < 10) { minute = "0" + minute; }
if (second < 10) { second = "0" + second; }
//var timeString = hour + ':' + minute + ':' + second + " " + ap;
var timeString = hour + ':' + minute + " " + ap;
document.getElementById("time").innerText=timeStri ng;
}

function changetime(interval,direction) {
//chamge hours or minutes up or down
var currentdisplay =
document.getElementById('time').innerText.toUpperC ase();
var times=currentdisplay.split(':');
var h_current = parseFloat(times[0]); //hours
var m_current = parseFloat(times[1]); //minutes
var len = currentdisplay.length;
var ap = currentdisplay.substring(len-2,len); //AM or PM
var h_new=h_current;
var m_new=m_current;
if (interval=='h') {
if (direction=='+') h_new += 1;
if (direction=='-') h_new -= 1;
}
if (interval=='m') {
if (direction=='+') m_new += 1;
if (direction=='-') m_new -= 1;
}
if (m_new > 59) {
h_new += 1;
m_new -= 60;
}
if (m_new < 0) {
m_new = 59;
h_new -= 1;
}
if (h_new == 0) {
h_new=12;
ap=flipampm(ap);
}
if (h_new > 12) {
h_new -= 12;
ap=flipampm(ap);
}
if (h_new < 10)
h_new= '0' + h_new;
if (m_new < 10)
m_new = '0'+m_new;
var timeString = h_new + ':' + m_new + " " + ap;
document.getElementById("time").innerText=timeStri ng;
return true;
}

function flipampm(ap) {
//swap am-pm
if (ap == 'AM')
ap='PM';
else
ap='AM';
return ap;
}
</script>
<title>change time</title>
</head>

<body onload="getClockTime()">
Adjust the Time<p>
<div name="time" id="time"></div>
<p>&nbsp;</p>
<p>
Hours
<input type="button" value=" - " onclick="changetime('h','-')">
<input type="button" value=" + " onclick="changetime('h','+')">
<br>
Minutes
<input type="button" value=" - " onclick="changetime('m','-')">
<input type="button" value=" + " onclick="changetime('m','+')">
</body>

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Randell D. | last post by:
Folks, I have a table of addresses and a seperate table with contact names - All addresses tie to one or more names - I would like to keep track of the number of names 'belonging' to an address...
2
by: Tom | last post by:
I am trying to store information into a table that has an auto increment field. There is currently no data in the table. Using the code below I cannot insert data into the table. I get an error...
8
by: ISmith | last post by:
Hi, I am trying to increment a field on a form after a record has been stored. It is a standard fumeric field which may contain some blocks of consecutive values in the table but is not meant to...
13
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an...
3
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe...
0
by: Matt | last post by:
I derived my own custom class from the datagrid class. I overrode the ProcessCmdKey Function, like this, to catch the up and down arrow keys: ====== Protected Overrides Function...
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.