Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get date with suffix st,th etc

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 180
    Comment on it

    While getting date in rails we do have a requirement where we need to show suffix attached to date like 2nd,3rd etc. There is no direct rails date function to get the same so in order to get date in the following format


    Thu, Nov 5th 2015
    

    We can do the following by twisting strftime function a bit like

    starts_at_date.try(:strftime,"%a, %b #{starts_at_date.try(:day).try(:ordinalize)} %Y") 
    


    Where starts_at_date is the date object from which we are trying to show the date in particular format. Thing to note here is #{starts_at_date.try(:day).try(:ordinalize)} which help you display 2nd , 3rd etc.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: