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 141817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:50:15+00:00 2026-05-11T07:50:15+00:00

I have two methods that I’m using as custom tags in a template engine:

  • 0

I have two methods that I’m using as custom tags in a template engine:

# Renders a <select> form field def select_field(options, selected_item, field_name):     options = [(str(v),str(v)) for v in options]     html = ['<select name='%s'>' % field_name]     for k,v in options:         tmp = '<option '         if k == selected_item:             tmp += 'selected '         tmp += 'value='%s'>%s</option>' % (k,v)         html.append(tmp)     html.append('</select>')     return '\n'.join(html)  # Renders a collection of <select> fields for datetime values def datetime_field(current_dt, field_name):     if current_dt == None:         current_dt = datetime.datetime.now()     day = select_field(range(1, 32), current_dt.day, field_name + '_day')     month = select_field(range(1, 13), current_dt.month, field_name + '_month')     year = select_field(range(datetime.datetime.now().year, datetime.datetime.now().year + 10), current_dt.year, field_name + '_year')     hour = select_field(range(1, 13), current_dt.hour, field_name + '_hour')     minute = select_field(range(1, 60), current_dt.minute, field_name + '_minute')     period = select_field(['AM', 'PM'], 'AM', field_name + '_period')     return '\n'.join([day, '/', month, '/', year, ' at ', hour, ':', minute, period]) 

As you can see in the comments, I’m attempting to generate select fields for building a date and time value.

My first question is, how do I make the day, month, hour, and minute ranges use two digits? For example, the code generates ‘1, 2, 3…’ while I would like ’01, 02, 03′.

Next, when the fields get generated, the values of the supplied datetime are not selected automatically, even though I’m telling the select_field method to add a ‘selected’ attribute when the value is equal to the supplied datetime’s value.

Finally, how do I get the 12-hour period identifier (AM/PM) for the supplied datetime? For the moment, I’m simply selecting ‘AM’ by default, but I would like to supply this value from the datetime itself.

  • 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. 2026-05-11T07:50:15+00:00Added an answer on May 11, 2026 at 7:50 am

    To turn an integer range into two digit strings:

    >>> range(13) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] >>> [ '%02d' % i for i in range(13) ] ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'] 

    Then to get the AM/PM indicator:

    >>> import datetime >>> current_dt = datetime.datetime.now() >>> current_dt datetime.datetime(2009, 2, 4, 22, 2, 14, 390000) >>> ['AM','PM'][current_dt.hour>=12] 'PM' 

    Voila!

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

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer find's -prune comes to mind, but it's a pain to… May 11, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer Go to File > Connection to bring up the data… May 11, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer Use LSCopyAllHandlersForURLScheme(CFSTR("http")) or LSCopyAllRoleHandlersForContentType(CFSTR("public.html"), kLSRolesViewer), or the set intersection of… May 11, 2026 at 6:45 pm

Related Questions

I have two methods that I'm using as custom tags in a template engine:
I have two methods that basically converts underlying checkboxes' text or tag as CSV
I have two related tables, MANY and ONE. An integer ONE_ID column on the
Suppose I have two instances of the same class. The class has a pointer
I have the following 3 classes Book Product SpecialOptions There are many Books, and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.