Need to custom format a ruby date object? - ruby

how do I format a ruby date as the following?
sep14.2016
It looks like the format is 3 characters of month followed by 2 characters of day with a period and a year.

Try This
Date.new(2016,9,14).strftime('%b%d.%Y')

d = Date.parse('14 September 2016')
to get your date object.
d.strftime('%b%d.%Y') #=> Sep14.2016
This will format your date like you wanted. Keep in mind d is not modified, so it is still a Date object. You need to assign the result of the strftime method to any variable.
You can use the downcase method to remove any capitalization.

Related

Transform a string into ISO datetime format | Freemarker

So I get the date and time which is, for example, 17.11.2021 and 12:44. Now I want to convert this date and time into the formate which Freemarker is using (yyyy-MM-dd hh:dd:mm:ss). However the problem here is that I can't convert it properly. I tried like:
${myDateTime?datetime.iso?string("yyyy-MM-dd HH:mm:ss")}
But this wont work. I always get an error message. Can anyone explain or show me the correct formation in this case ?
Assuming myDateTime is a String (and not already a Date object), and it looks like 17.11.2021 12:44, you can convert it to ISO format like this:
${myDateTime?datetime("dd.MM.yyyy HH:mm")?string.iso}
Above myDateTime?datetime("dd.MM.yyyy HH:mm") converts the string to a Date (which is a format-independent representation), and then ?string.iso formats the Date with ISO format.
Note that it would be much cleaner if myDateTime was a Date, not a String. Then you just do this: ${myDateTime?string.iso}

Get the current date/time in Ruby as string without delimiters?

Is there any good way to get the current date/time in Ruby as a string without separators, not having to use Time.now.strftime('%Y%m%d'), etc.?
The output I'm looking for is something like "20151002112001" or similar, with all digits and no separators, human-readable form, not Unix time.
I found out that by using Active Support, this can be easily done:
require 'active_support/core_ext/time/conversions'
Time.now.to_formatted_s(:number) # => "20151002112419"
Since I already depend on Active Support, this turned out to be the quickest and easiest way to get this done by far.
You can use .to_i on a Time object to get a number representing Unix-time.
You can convert Date or DateTime objects to Time with .to_time. For example:
string = "#{Date.today.to_time.to_i}"
# "1443769200"
If you need to get certain attributes but don't like strftime, you can use methods of Date. For example, you might do something like
date = Date.today
string = "#{date.year}-#{date.month}-#{date.day}"
# "2015-10-2"
This all works without Active Support, but you still need to require 'date'.

What's the correct date format to turn this string into a date?

I'm consuming a JSON API, which returns dates, such as the following: 2012-11-13T17:32Z. What's the correct value for a formatString for an associated NSDateFormatter instance?
Edit:
I tried #"yyyy-MM-dd'T'hh':'mm'Z'".
You don't need the quotes around the colon, and you should be using capital H's for the hours.

Replacing manually written date with a string containing it

I have these 2 things I am working with:
CSV.foreach('datafile.csv','r') {|row| D_Location << row[0]}
puts Date.new(2003,05,02).cwday
In the first line I would like to change the datafile.csv to something like a string so I can change one string and it changes for all of these codes. I have many, each controlling 1 csv column.
In the second one I would like to replace the actual date written, and replace it with a string. This is so that can be automatic, because the string will be generated based on other criteria.
I trust the mods will ban me if I'm being too much of a noob hehe. Then I'll toughen up and find these answers myself eventually. But so far I've solved a lot, but not this. Thanks in advance!
Make a function which takes in a string representing a weekday, and returns a number. Call this function later in your code:
Date.new(2003, 05, yourfun('Tuesday')).cwday
For the first part of your question, you're already working with a string. I think what you mean is that you want it to be in a variable:
csv_file = 'datafile.csv'
CSV.foreach(csv_file,'r') {|row| D_Location << row[0]}
For the second part of your question, Date.parse() works with strings, but they need to be in a format that it can recognize. If your date strings use commas, you can replace them with hyphens:
date_str = "2003,05,02"
Date.parse(date_str.gsub(",", "-")).cwday # => 5
It's not clear where your date strings will be coming from or what format they'll be in, but the general concepts you need to understand are that you can use variables, and that you can transform strings.

how to format string output using % in ruby?

I want to format the output of a string in ruby. I can do it using % but I cannot figure it out.
Instead of 201107070928 I want to output 2011\07\070928
puts "%.4s\\%.2s\\%s" % "201107070928"
gives me an error:`%': too few arguments (ArgumentError)
That's not how you use it for formatting a date. The way to use % formatting is:
puts "%.4s\\%.2s\\%s" % ["1","2","3"]
So, you need multiple parameters - one for each of the format specifiers.
If you need to print & format a date from a string-date, first convert the string to a date/time object and then use strftime:
Presuming the input is date and a time:
Time.parse("201107070928").strftime("%Y\\%m\\%d%H%M").
I'm going to put this in an answer rather than a comment just because I'm more impressed with the possibility. Using String#unpack, we can handle a 4 digit / 2 digit / anything with:
"201107070928345345345".unpack("a4a2a*").join('/')
=> "2011/07/070928345345345"

Resources