Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6068109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:40:49+00:00 2026-05-23T09:40:49+00:00

i have created two text box which will take time as input from user

  • 0

i have created two text box which will take time as input from user i want when user press tab key the value of text box converted to 24 hour format
i have written a method

    function getFormattedTime(value) {
    var time = j$.trim(value);
    var len = time.length;
    var result = "";

    var pos = time.indexOf(":");
    if (pos < 0) {
        pos = time.indexOf(".");
    }
    if (time.indexOf('m') < 0 && pos < 0 && parseInt(time) > 12
            && parseInt(time) < 12) {
        time = time + 'm';
    }
    if (time.indexOf('m') > 0) {
        time = parseFloat(time.substring(0, time.indexOf('m')));
        if (isNaN(time)) {
            window
                    .alert("Hours entered is invalid [00:00] or [00.00] or [00h] or [00m]");
            return "";
        } else {
            if (time > 59) {
                result = parseInt(time / 60);
                result = result + ":" + (time - (result * 60));
            } else {
                result = "00:"
                        + (parseFloat(time) <= 9 ? "0" + time : time);
            }
        }
        return result;
    }
    var hour = "00";
    if (time.charAt(0) == '0') {
        hour = '0'
                + (parseFloat(time.substring(1, 2)) ? parseFloat(time
                        .substring(1, 2)) : '0');
    } else {
        hour = (parseFloat(time.substring(0, 2)) ? parseFloat(time
                .substring(0, 2)) : '00');
        hour = (hour > 24 ? 24 : (hour < 10 ? (hour == 0 ? '00' : '0'
                + hour) : hour));
    }
    var mins = "00";
    if (pos != -1) {
        if (time.charAt(pos + 1) == '0') {
            mins = '0'
                    + (parseFloat(time.substring(pos + 2, pos + 3))
                            ? parseFloat(time.substring(pos + 2, pos + 3))
                            : '0');
        } else {
            mins = (parseFloat(time.substring(pos + 1, 5))
                    ? parseFloat(time.substring(pos + 1, 5))
                    : '00');
            mins = (mins >= 60 ? 59 : (mins < 10 ? (mins >= 6 ? 59 : mins
                    + '0') : mins));
            mins = (hour == 24 ? 45 : mins);
        }
    }
    result = hour + ":" + mins;
    return result;
}

It is working fine for following format

1.) 0 – 00:00
2.) 9.3 – 09:30
3.) 13.3 – 13:30
4.) 9 – 09:00

but when i enter

1352 it should give me 13:52 but it is giving 13:00
where i did wrong any help ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:40:50+00:00Added an answer on May 23, 2026 at 9:40 am

    What happens when pos is -1? Well, when pos is -1, you’re left with:

    var mins = "00";
    //...
    result = hour + ":" + mins;
    

    How does pos end up being -1? Well, any time that time doesn’t contain a colon or period:

    var pos = time.indexOf(":");
    if (pos < 0) {
        pos = time.indexOf(".");
    }
    

    In particular, mins will be "00" when time is 1352. You need an else branch on your final if to handle the case when there is no colon or period.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have created two objects from class foo and now want to
I have made one custom user control (search text box), which basically consists of
alt text http://home.elka.pw.edu.pl/~pkolodzi/Capture.PNG I have created the IAdder interface with two methods: PassA and
I have a user table in my database which contains two columns FirstName and
I'm trying to build a page which will have a box-like layout..A top banner
I have created two forms in my Windows Application. One Form acts as a
I have created two files: tunables.h #ifndef TUNABLES_H #define TUNABLES_H void tunables_load_conservative(); void tunables_load_aggressive();
I have created two JavaScript files. One file is "validators.js" and the other is
I'm having a problem with mixing managed and unmanaged code. I have created two
I have created an item swapper control consisting in two listboxes and some buttons

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.