const today = new Date(); function formatDate(date, format) { // } formatDate(today, 'mm/dd/yy'); You need to replace the strings "mm", "dd", "yy" with the respective month, day and year values from the format string passed in the argument. To do that you can use the replace() method like shown below: format.
First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.
The easiest way is to press Ctrl + 1 key on your keyboard. Now in the Format cells window, go to the Number tab. Then from the Category list, choose the Custom option. After that, in the Type input box put dd/mm/yyyy hh:mm: ss.
The formatting can be done like this (I assumed you meant HH:MM instead of HH:SS, but it's easy to change):
Time.now.strftime("%d/%m/%Y %H:%M")
#=> "14/09/2011 14:09"
Updated for the shifting:
d = DateTime.now
d.strftime("%d/%m/%Y %H:%M")
#=> "11/06/2017 18:11"
d.next_month.strftime("%d/%m/%Y %H:%M")
#=> "11/07/2017 18:11"
You need to require 'date'
for this btw.
require 'date'
current_time = DateTime.now
current_time.strftime "%d/%m/%Y %H:%M"
# => "14/09/2011 17:02"
current_time.next_month.strftime "%d/%m/%Y %H:%M"
# => "14/10/2011 17:02"
time = Time.now.to_s
time = DateTime.parse(time).strftime("%d/%m/%Y %H:%M")
for increment decrement month use << >> operators
examples
datetime_month_before = DateTime.parse(time) << 1
datetime_month_before = DateTime.now << 1
For date:
#!/usr/bin/ruby -w
date = Time.new
#set 'date' equal to the current date/time.
date = date.day.to_s + "/" + date.month.to_s + "/" + date.year.to_s
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
puts date
#output the date
The above will display, for example, 10/01/15
And for time
time = Time.new
#set 'time' equal to the current time.
time = time.hour.to_s + ":" + time.min.to_s
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display hour and minute
puts time
#output the time
The above will display, for example, 11:33
Then to put it together, add to the end:
puts date + " " + time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With