When managing the server, I often write a script to get the spit out log by date and batch process it with cron. At that time, I often write a script to get the date in 2 digits (01,02,03, ..., 10, ...), so make a note in various languages.
javascript
You can get the time by creating a Date instance
<script>
var date = new Date();
var dd = ("0"+date.getDate()).slice(-2);
</script>
Add "0" to the value obtained by getDate
By using slice (-2), only the last 2 digits are acquired.
So, even if it becomes "011" such as 11th, only the last 2 digits will be acquired.
"11" is displayed
Ruby
You can get the current time with the Time # now method. The Time :: strftime method allows you to convert the time according to the specified format.
■ Format format
%c
Date and time
%d
Day(01-31)
%H
24-hour clock(00-23)
%I
12-hour clock(01-12)
%j
Total date of the year(001-366)
%M
Minutes(00-59)
%m
Number representing the month(01-12)
Example
day = Time.now
p day.strftime("Now, %m %d") #=> "Now, 01-02"
Time.now.strftime("%Y-%m-%d %H:%M:%S") #=> "2017-01-02 00:31:21"
On the contrary, when you want to express with one digit
day = Time.now
p day.year #=> 2017
p day.month #=> 1
p day.day #=> 2
Python
Basically the same as ruby
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%m-%d")
'01-02'
Format can be specified with date'+ format'
date '+%m' //01
date '+%d' //02
Recommended Posts