Table of contents
1.
Introduction
2.
Web2py
3.
On date, datetime, and time format
4.
Example
4.1.
Problem 
4.2.
Solution
5.
Frequently Asked Questions
5.1.
What is Web2py?
5.2.
Why Web2py is used?
5.3.
Can web2py be used with Python 3?
5.4.
How to change the date format?
5.5.
Which is preferable, Django or web2py?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

On Date, Datetime, and Time Format in web2py

Author yuvatimankar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

web2py is a free, open-source web framework for agile development that implies database-driven web apps; web2py is written and programmable in Python. web2py is a framework for full-stack; it is comprised of all the components a developer requires to build a fully functional web application.

Web2py

web2py follows MVC (Model-View-Controller) pattern for running web applications. The model here is part of the application, which includes the logic of the data, the view here is a part of the application that helps in rendering the data display to end-users, and the controller here is a part of the application that handles user interaction. 

Web2py is built by keeping security in mind and is its very first concern for it. As a Back-End framework, web2py comes with a built-in Data Abstraction Layer that helps developers communicate with different SQL DataBases such as SQLite, PostgreSQL, MySQL, MSSQL, Oracle, IBM DB2, etc.

On date, datetime, and time format

As with any other framework, web2py also has some formats in a different areas, so we will discuss the date, datetime, and time format in this section.web2py consists of three different representations for each field types date, time, and datetime:

  • the database representation
  • the internal web2py representation
  • the string representation in forms and tables

In web2py, the database representation is an internal affair/issue and does not affect the code. In web2py, internally, they are stored as datetime.datetime, datetime.date, and datetime.time object, respectively, and they can be customized as below:


for page in db(db.page).select():
    print page.title, page.created_on.day, page.created_on.month, page.created_on.year

 

When we convert dates to strings, they are converted using the ISO representation:

%Y-%m-%d %H:%M:%S


Though yet, this representation is generalized, and we can use the admin translation page to change the format into another one. For example:

%m/%d/%Y %H:%M:%S

 

Note that by default, English is not translated because web2py assumes the apps are written in English. If we want internationalization to work for English, we need to design the translation file, and we need to declare that the application’s current language is different than English, for example:

T.current_languages = ['null']

Example

Problem 

To show month-day-year format using web2py.

Solution

db.define_table('time',
   Field('date_made', 'datetime', default = request.now, requires = IS_DATE(format=('%d-%m-%Y'))) )    

Frequently Asked Questions

What is Web2py?

Web2Py is an open-source, free full-stack framework written in Python that helps rapidly develop fast, scalable, secure, and portable database-driven web-based applications.

Why Web2py is used?

Web2py is mainly used to program dynamic websites using Python as a programming language. It contains all the tools, components, and APIs essential to building a fully functional web application.

Can web2py be used with Python 3?

Web2py functions on Python 2.7, Python 3, with CPython (the C implementation), and PyPy (Python written in Python).

How to change the date format?

For changing the date format, we have to use the syntax: import datetime

now = datetime.datetime.now()

 

#You probably need something like the below --

date = now.strftime("%d %b %Y")

Which is preferable, Django or web2py?

Due to its smaller size, short learning curve, and lack of project-level configuration files, Web2py is preferred over Django. Compared to PHP-based frameworks and Java-based frameworks, Web2py has a significantly more explicit syntax than PHP-based and Java-based frameworks, making this application easier to comprehend, maintain, and create.

Conclusion

In this article, we have discussed the web2py introduction, and then we saw what the date, datetime, and time format in web2py are. We started with the introduction of web2py, and then, with the help of some examples, we saw date, datetime, and time format.

Also, visit our Guided Path in  Coding Ninjas Studio to learn about Web2py. If you are preparing for an interview, visit our Interview Experience Section and interview bundle for placement preparations. Upskill yourself in PythonKivyBackend Web TechnologiesSQL, MongoDB, Data Structures and Algorithms, JavaScript,  System Design, and much more!. Please upvote our blogs if you find them engaging and helpful! I wish you all the best for your future adventures and Happy Coding. 

Useful links: Web2pyKivyRuby vs. Python. You can also refer to the Web2py IntroductionWeb2py InstallationWeb2py ApplicationWeb2py ToolWeb2py Capacity Planning, and Troubleshooting in Web2py.   

Live masterclass