Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script

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'

Shell script

Format can be specified with date'+ format'

date '+%m'  //01
date '+%d'  //02

Recommended Posts

Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
How to get the number of digits in Python
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
How to get the date and time difference in seconds with python
Get the return value of an external shell script (ls) with python3
Get the update date of the Python memo file.
How to get a list of files in the same directory with python
Script to get the expiration date of the SSL certificate
Calculate the square root of 2 in millions of digits with python
Get the source of the page to load infinitely with python.
Get the script path in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
Process the contents of the file in order with a shell script
Output the contents of ~ .xlsx in the folder to HTML with Python
Get the title and delivery date of Yahoo! News in Python
PhytoMine-I tried to get the genetic information of plants with Python
I tried to get the authentication code of Qiita API with Python.
I tried to get the movie information of TMDb API with Python
I made a program to check the size of a file in Python
I tried to open the latest data of the Excel file managed by date in the folder with Python
[Python] Get the files in a folder with Python
Get the caller of a function in Python
Check the date of the flag duty with Python
[Python] Get the last updated date of the website
How to get the files in the [Python] folder
Python script to get note information with REAPER
How to pass the execution result of a shell command in a list in Python
Compare the sum of each element in two lists with the specified value in Python
Python script to get a list of input examples for the AtCoder contest
Note: How to get the last day of the month with python (added the first day of the month)
[Introduction to Python] How to get the index of data with a for statement
How to get the variable name itself in python
From the initial state of CentOS8 to running php python perl ruby with nginx
A memo connected to HiveServer2 of EMR with python
Template of python script to read the contents of the file
Get the value of a specific key in a list from the dictionary type in the list with Python
How to identify the element with the smallest number of characters in a Python list?
[Python] Get the numbers in the graph image with OCR
An easy way to pad the number with zeros depending on the number of digits [Python]
Get the return code of the Python script from bat
Convert the image in .zip to PDF with Python
Try to get the contents of Word with Golang
Get date in Python
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
Get date with python
A memo of misunderstanding when trying to load the entire self-made module with Python3
Get the operation status of JR West with Python
How to count the number of occurrences of each element in the list in Python with weight
Get the URL of the HTTP redirect destination in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Get in touch with functional programming in JavaScript or Python 3
linux / c> link> Get the execution result of the shell command in the C program> I was taught how to use popen ()
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
How to handle JSON in Ruby, Python, JavaScript, PHP
Let's see how to count the number of elements in an array in some languages [Go, JavaScript, PHP, Python, Ruby, Swift]
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
Get the value of a specific key up to the specified index in the dictionary list in Python
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
Recursively get the Excel list in a specific folder with python and write it to Excel.
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
How to pass the execution result of a shell command in a list in Python (non-blocking version)