[RAILS] I want to display the number of orders for today using datetime.

First, let's see the movement of datetime with "$ rails c".

irb


#Get current time
[1] pry(main)> dt=DateTime.now
=> Wed, 23 Dec 2020 05:34:58 +0000

#Process only date information with parse
[2] pry(main)> today=Date.parse(dt.strftime("%Y/%m/%d %H:%M:%S"))
=> Wed, 23 Dec 2020


#Define a date
[3] pry(main)> dt2 = DateTime.new(2020,12,23,5,6,7)     
=> Wed, 23 Dec 2020 05:06:07 +0000

#Process only date information with parse
[4] pry(main)> the_day=Date.parse(dt2.strftime("%Y/%m/%d %H:%M:%S"))
=> Wed, 23 Dec 2020

#today == the_day
[5] pry(main)> the_day == today
=> true


When you want to display today's order

** Status settings ** I want to display today's number of orders on the top screen of the administrator of the EC site. Order model: Order

orders_controller



    def top

        dt=DateTime.now  #First, get today's date. Like this(=> Wed, 23 Dec 2020 05:34:58 +0000)
        today=Date.parse(dt.strftime("%Y/%m/%d %H:%M:%S"))  #Extract only the date
        
        
        orders=Order.all  #For the time being, I will pull all the order information.
        
        @sum=0  #For the total number. * Note that it is not initialized separately.
                #@sum = @sum +When set to 1, "@What is sum? I get the error.

        orders.each do |x|   #Since it is a science system, it is defined as x.
            dt2=x.created_at
            the_day=Date.parse(dt2.strftime("%Y/%m/%d %H:%M:%S"))
            
            if today == the_day
                @sum = @sum + 1
            end
            
        end
        
    end

Recommended Posts

I want to display the number of orders for today using datetime.
I want to display the name of the poster of the comment
I want to narrow down the display of docker ps
The story of Collectors.groupingBy that I want to keep for posterity
I want to output the day of the week
I want to var_dump the contents of the intent
I want you to use Enum # name () for the Key of SharedPreference
I want to judge the range using the monthly degree
I want to know the answer of the rock-paper-scissors app
I want to call the main method using reflection
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
[Must-see for beginners] I changed the display of the posting time to Japanese time [l method]
[Rails] I want to display the link destination of link_to in a separate tab
I want to reduce the number of unnecessary queries. From considering counter_cache to introducing counter_culture.
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
I want to call a method and count the number
I want to use the Java 8 DateTime API slowly (now)
[Rails] I want to display "XX minutes ago" using created_at!
I want to return multiple return values for the input argument
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to control the display of the upper management navigation bar (Control menu) in Liferay 7 / DXP
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
I want to create a chat screen for the Swift chat app!
I want to understand the flow of Spring processing request parameters
How to get the contents of Map using for statement Memorandum
I want to limit the input by narrowing the range of numbers
I want to display the images under assets/images in the production environment
I want to control the default error message of Spring Boot
I tried to display the calendar on the Eclipse console using Java.
I want to change the value of Attribute in Selenium of Ruby
I want to graph the number of photo AC downloads [Scraping implementation] ~ 10 lines per day coding ~
I want to display background-ground-image on heroku.
How to determine the number of parallels
I want to display images with REST Controller of Java and Spring!
Glassfish tuning list that I want to keep for the time being
I want to know the JSP of the open portlet when developing Liferay
Practice of Java programming basics-I want to display triangles with for statements ①
[Ruby] I want to extract only the value of the hash and only the key
How to switch the display of the header menu for each transition page
I want to pass the argument of Annotation and the argument of the calling method to aspect
[Ruby] I want to display posted items in order of newest date
I want to display an error message when registering in the database
Practice of Java programming basics-I want to display triangles with for statements ②
I want to get the field name of the [Java] field. (Old tale tone)
I want to graph the number of photo AC downloads [MySQL ring cooperation] ~ Coding 10 lines a day ~
I tried using Docker for the first time
[RxSwift] I want to deepen my understanding by following the definition of Observable
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to control the start / stop of servers and databases with Alexa
How to display the amount of disk used by Docker container for each container
Development memo ~ I want to display only the first image posted multiple times ~
[Ruby] Code to display the day of the week
I want you to use Scala as Better Java for the time being
I want to create the strongest local development environment using VSCode Remote Containers
How to display the result of form input
I want to truncate after the decimal point
I want to separate the handling of call results according to the API caller (call trigger)
I want to see the contents of Request without saying four or five