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

  • SEARCH
  • Home
  • 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 1105937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:42:49+00:00 2026-05-17T01:42:49+00:00

My application fetches information from an API. Unfortunately their data isn’t very pretty. This

  • 0

My application fetches information from an API. Unfortunately their data isn’t very pretty. This is what the strings can look like:

2
2:30
10.00
2 am
3:30 pm
10:00pm est
10:00pm PT/EST
10:00pm (Central)
10:00, 11:00 & 12:00

I mean, they can pretty much be anything 🙂

I need the format to be 12:00 (HH:MM) !

Here is what I’ve got so far, and it works pretty OK, but I’m no RegExp-guru 🙂 I think there is a much faster shortcut getting there.

(Ruby code. Gsub = Replace)

def universal_hour(time)

  #replacing & (10:00 & 18:00) and dots (10.00)
  formatted_time = time.upcase.gsub("&", ",").gsub(".", ":")

  #midnight = 00:00
  formatted_time = formatted_time.gsub("midnight", "00:00")

  #removing everything else (10:00 am PST whatever), whitespaces and tailing punctuation
  formatted_time = formatted_time.gsub(/[a-z]+|[A-Z]+|\(|\)|\s|.*:$|.*,$|.*-$/, "")

  #make these formats: 10, 2, 3:30, into: HH:MM
  formatted_time = "#{formatted_time}:00" if formatted_time.match(/^\d{2}$/)
  formatted_time = "0#{formatted_time}:00" if formatted_time.match(/^\d{1}$/)
  formatted_time = "0#{formatted_time}" if formatted_time.match(/^\d{1}:\d{2}$/)

  #Some are dates (2007-01-24)
  formatted_time = "" if formatted_time.match(/^\d{4}-\d{2}-\d{2}$/)

  #Some weird things (10/9)
  formatted_time = "" if formatted_time.match(/\//)

  #Be safe: If it's still doesn't contain a number, remove it
  formatted_time = "" unless formatted_time.match(/\d/)

  #10-30 -> 10:30
  formatted_time = formatted_time.gsub("-", ":") if formatted_time.match(/^\d+-\d{2}$/)

  #2300 -> 23:00
  if formatted_time.match(/^\d{4}$/)
    h = formatted_time[0..1]
    m = formatted_time[2..3]
    formatted_time = "#{h}:#{m}"
  end

  return formatted_time
end
  • 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-17T01:42:50+00:00Added an answer on May 17, 2026 at 1:42 am

    Instead of using regex, why not use Ruby’s ability to parse and format time?

    t = Time.parse("3:30 pm").strftime("%H:%M") # 15:30
    

    The only problem with this is that it won’t cover some of your corner cases (like strings with timezones, or “midnight”). For that, you can use a time parsing gem, like Chronic.

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

Sidebar

Related Questions

I have a web application which fetches information from a web service. It works
I am learning Python by building a simple PyGTK application that fetches data from
I am making an application that fetches data from internet like web service,so it
I have a website which fetches information from RSS feeds periodically (well, currently manually,
In my application I keep a LINQ object fetched from the database, and regularly
Application has an auxiliary thread. This thread is not meant to run all the
My application dynamically loads assemblies at runtime from specific subfolders. These assemblies are compiled
My application has a need to let the user choose a date from a
I'm writing tests for a Django application that uses an external data source .
Until now I've been used to using DAOs to retrieve information from databases. Other

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.