info@rranktec.com
+91 80 65655677
Logo
  • Home
  • About Rank
    • Our Latest Project

      Show Me

      We combine deep technology expertise and industry-specific insights into how technology impacts people and processes to deliver solutions that help clients achieve business outcomes with agility, speed and certainty.

      Rank Pages

      • About
      • Official Fedena Technology Partner
      • Rank Team
      • Customer Testimonials

      About Us

      Rank technologies is an Leading IT solutions provider based in Bangalore, India. providing end-to-end solutions with a team of vast expertise and strong technical proficiency. Beyond development of business solutions, we help you in deploying, managing and scaling them. No headaches for you!

  • Services
    • Fedena Services
    • Design & Development
    • Consultancy Services
    • Mobile Applications Development
  • Products
    • EasyAd
    • Fedena
    • DoctorInHome
    • Admonk
    • Ashvayana
    • Honudi App
    • Yojane
  • Blogs
  • Contact Us

Rank Blogs

  1. Home
  2. Rank Blogs
04-06-2015 Date-Time   Ruby-o-rails 

Date-Time Format in Ruby

In Ruby, you have to format or convert time from one format to other many a times. You can use library method strftime available to perform formatting operations.

Once I have faced this error in my terminal window,

Syntax of strftime: strftime( format )

Returns: Result in requested format

First of all, let us create a new variable in which we get current time to be formatted in various formats,

 
t = Time.now > 2013-09-12 22:49:27 +0530

Now, we have variable t holding current time value, now below are the examples of,

Most Commonly used DateTime Formats

CodeOutputDescription
t.strftime(“%H”) => “22” # Gives Hour of the time in 24 hour clock format
t.strftime(“%I”) => “10” # Gives Hour of the time in 12 hour clock format
t.strftime(“%M”) => “49” # Gives Minutes of the time
t.strftime(“%S”) => “27” # Gives Seconds of the time
t.strftime(“%Y”) => “2013” # Gives Year of the time
t.strftime(“%m”) => “09” # Gives month of the time
t.strftime(“%d”) => “12” # Gives day of month of the time
t.strftime(“%w”) => “4” # Gives day of week of the time
t.strftime(“%a”) => “Thu” # Gives name of week day in short form of the
t.strftime(“%A”) => “Thursday” # Gives week day in full form of the time
t.strftime(“%b”) => “Sep” # Gives month in short form of the time
t.strftime(“%B”) => “September” # Gives month in full form of the time
t.strftime(“%y”) => “13” # Gives year without century of the time
t.strftime(“%Y”) => “2013” # Gives year without century  of the time
t.strftime(“%Z”) => “IST” # Gives Time Zone of the time
t.strftime(“%p”) => “PM” # Gives AM / PM of the time

These are the almost all formats that are required.

Combinations:

You can try these formats in combination too,
For example,

 
 
t.strftime("%H:%M:%S")> "22:49:27"

Code to Print All Formats

For ‘a’ to ‘z’
CodeOutput
t.strftime(‘%a’) => Thu
t.strftime(‘%b’) => Jan
t.strftime(‘%c’) => Thu Jan 23 16:38:02 2014
t.strftime(‘%d’) => 23
t.strftime(‘%e’) => 23
t.strftime(‘%f’) => %f # Not Useful
t.strftime(‘%g’) => 14
t.strftime(‘%h’) => Jan
t.strftime(‘%i’) => %i # Not Useful
t.strftime(‘%j’) => 023
t.strftime(‘%k’) => 16
t.strftime(‘%l’) => 4
t.strftime(‘%m’) => 01
t.strftime(‘%n’) => # Not Useful
t.strftime(‘%o’) => %o
t.strftime(‘%p’) => PM
t.strftime(‘%q’) => %q
t.strftime(‘%r’) => 04:38:02 PM
t.strftime(‘%s’) => 1390475282
t.strftime(‘%t’) => # Not Useful
t.strftime(‘%u’) => 4
t.strftime(‘%v’) => 23-JAN-2014
t.strftime(‘%w’) => 4
t.strftime(‘%x’) => 01/23/14
t.strftime(‘%y’) => 14
t.strftime(‘%z’) => +0530

For ‘A’ to ‘Z’
CodeOutput
t.strftime(‘%A’) => Thursday
t.strftime(‘%B’) => January
t.strftime(‘%C’) => 20
t.strftime(‘%D’) => 01/23/14
t.strftime(‘%E’) => %E # Not Useful
t.strftime(‘%F’) => 2014-01-23
t.strftime(‘%G’) => 2014
t.strftime(‘%H’) => 16
t.strftime(‘%I’) => 04
t.strftime(‘%J’) => %J # Not Useful
t.strftime(‘%K’) => %K # Not Useful
t.strftime(‘%L’) => 485
t.strftime(‘%M’) => 38
t.strftime(‘%N’) => 485141000
t.strftime(‘%O’) => %O # Not Useful
t.strftime(‘%P’) => pm
t.strftime(‘%Q’) => %Q # Not Useful
t.strftime(‘%R’) => 16:38
t.strftime(‘%S’) => 02
t.strftime(‘%T’) => 16:38:02
t.strftime(‘%U’) => 03
t.strftime(‘%V’) => 04
t.strftime(‘%W’) => 03
t.strftime(‘%X’) => 16:38:02
t.strftime(‘%Y’) => 2014
t.strftime(‘%Z’) => IST

 


Sanju B

Being Experienced Ruby on Rails Developer, worked at different aspects of web development. I am comfortable working in Back-end as well as Front-end side. I have worked on many Web Applications and contributed to the Creating Features, Doing Performance Optimization, Database Modelling, Creating Technical Documents. Also worked with different Gems and Plugins to integrate as well as customize them as per project requirements including all the Social Networking and Payment Gateway APIs.

  •  sanju@ilovebangalore.in
  • in.linkedin.com/in/sanjumyh

Search

Categories

  • Payment1
  • Express Checkout1
  • Paypal1
  • Devise4
  • Authentication4
  • Google Login1
  • SMS Alert1
  • Java Install1
  • DEPLOY A JAVA APP1
  • Saas1
  • Java in AWS Using Tomcat1
  • PAYU Integration1
  • Paginate1
  • Ransack Search1
  • Sun Spot1
  • DEPLOY RAILS APP AWS1
  • E-commerce1
  • spree1
  • USSD1
  • MySQL1
  • Facebook Login1
  • Rjb Gem Excel File1
  • Date-Time 1
  • Rails with Oracle1
  • Json1
  • Wicked PDF1
  • Heroku3
  • SMTP1
  • override1
  • Android SDK and App1
  • Ruby on Rails1
  • version control1
  • Redmine Plugins1
  • Offers1
  • Project Management1
  • Helpy Modern Helpdesk1
  • Spree1
  • offshore Development Centre1

Tags

Ruby-o-rails 

Contact

  • Rank Technologies.
  • New Bengaluru International Airport road, Bengaluru North-562157
  • info@rranktec.com
  • +91 80 65 655 677
  • www.rranktec.com

Latest Work

Newsletter

Get in touch with us today, if you think Rank could help you meet your requirements.


© Rank Technologies. 2017 All right reserved.

  • Home
  • Services
  • Products
  • Blog
  • Contact