This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Changing Military Time to Normal Time (JavaScript)

 
Ranch Hand
Posts: 511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to change the format of hours from Military Time to normal time, in JavaScript?
For example:
13:40
to
1:40 p.m. ???
Kindest regards,
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See sample code below and change it as per your need.
// var now = new Date(); Current date
var now = new Date("Feb 1, 1999 11:20:34" // Your pass your date/time here
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":" + minutes
timeValue += ((seconds < 10) ? ":0" : ":" + seconds
timeValue += (hours >= 12) ? " P.M." : " A.M."
alert( timeValue);
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, you could use the JavaScript Date class. It will automatically handle the military time conversion including adjusting the time zone from Zulu to the local zone as well as converting to the local time format.
Tom Blough
 
Unnsse Khan
Ranch Hand
Posts: 511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
Can you provide a simple example? Me and other developers looked at the reference and couldn't figure out how to use it for the conversion...
Many, many thanks!
 
Unnsse Khan
Ranch Hand
Posts: 511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu,
Thanks for the code snippet... It doesn't work when the time is 0:30:32 a.m. (in normal time-> 12:30 a.m.)......
Yours truly,
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a quick fix.....

[ February 12, 2004: Message edited by: Eric Pascarello ]
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You may have to mess with the separator param for the split method depending on your Locale information. The above works in the US. You can also specify a timezone in the input and it will offset to the local time automatically. I.e. "0:30:32 PST" --> "3:30:30 AM" (I'm EST)
Tom
[ February 12, 2004: Message edited by: Tom Blough ]
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!