How to change date from "mm-dd-yy" to "yy-mm-dd"? - ruby

date = "21-12-2013"
in a db table, I have a date column with "20131221", and I need to compare the date. Is the format wrong? How can I change the format form dd-mm-yy to yy-mm-dd?

Using regular expression (String#sub):
date = "21-12-2013"
date.sub(/(\d+)-(\d+)-(\d+)/, '\3-\2-\1')
# => "2013-12-21"
Using DateTime::strptime and DateTime#strftime:
require 'date'
DateTime.strptime(date, '%d-%m-%Y').strftime('%Y-%m-%d')
# => "2013-12-21"

Related

How to convert date time to UNIX in PowerBI for Desktop

I am new to powerBI I want to convert DateTime.LocalNow() to UNIX Timestamp
Use Duration.TotalSeconds
Duration.TotalSeconds(DateTime.LocalNow() - DateTime.FromText("1/1/1970"))
You can use this function:
let
fun_DateTimeToUnix = (optional DateTime as nullable datetime) as number =>
let
default_DateTime = if DateTime is null then DateTime.From(DateTimeZone.UtcNow()) else DateTime,
UnixDateTime = Duration.TotalSeconds(default_DateTime - DateTime.FromText("1/1/1970"))
in
UnixDateTime
in
fun_DateTimeToUnix
As shown:

[ruby]Get file, parse text and create date object

I have a raw .txt file formatted as such:
01.01.2017;New Year
16.04.2017;Easter
25.12.2017;Christmas
(Sidenote: dates are formatted as dd.mm.yyyy)
I'm trying to read this file, slice the text per line and make a hash out of it, with the key being the date, and its value the name of the corresponding public holiday.
I've already gotten so far:
holidays = Hash[*File.read('holidays.txt').split(/;|\n/)]
This results in the dates being set as strings, not date objects.
Any ideas as to how I could then transform these strings to Date (or DateTime) objects?
P.S.: I'm only using Ruby, so no Rails helpers...
Something like this
holidays = File.read('holidays.txt').split(/\n/).map do |row|
date, holiday_name = row.split(';')
date = Date.parse(date, '%d.%m.%Y')
[date, holiday_name]
end.to_h
=> {
#<Date: 2017-01-01 ((2457755j,0s,0n),+0s,2299161j)> => "New Year",
#<Date: 2017-04-16 ((2457860j,0s,0n),+0s,2299161j)> => "Easter",
#<Date: 2017-12-25 ((2458113j,0s,0n),+0s,2299161j)> => "Christmas"
}

How do I iterate through each date between two defined dates in ruby?

I have two dates, the begins_at (datetime) and the ends_at (datetime). I now simply want to iterate through every date between these two (including the ends_ and begins_at dates).
begins_at = Date.strptime("12/10/2014", "%m/%d/%Y")
ends_at = Date.strptime("12/20/2014", "%m/%d/%Y")
//iterate through all dates in this range kind of like this:
range = DateRange(begins-at,ends-at)
range.DateTime.each do |date|
....
end
Does anyone have an idea how I could achieve this?
You should use Range:
(begins_at..ends_at).each do |date|
# ...
end

Laravel - after date validation with date from rules set

I'm trying to validate two dates to ensure one is greater than the other using the after validate rule. I have my rules set like this:
$rules = array(
'date_from' => 'required|date_format:"d/m/Y"',
'date_to' => 'required|date_format:"d/m/Y"|after:date_from',
// more rules ...
);
When using the following values:
date_from = "01/06/2014" and date_to = "01/06/2014" OR date_to = "12/06/2014" everything is hunky dory, however.. using anything above 12 for day fails i.e. date_to = "13/06/2014" to date_to = "31/06/2014"
I've also tried this and it gives the same results:
$dateFromForm = Input::get('date_from');
$rules = array(
'date_from' => 'required|date_format:"d/m/Y"',
'date_to' => 'required|date_format:"d/m/Y"|after:' . $dateFromForm,
);
Quite clearly to me it's reading the day as the month, any ideas what I'm doing wrong here?
Thanks
If you look at the Laravel docs - it says
The dates will be passed into the PHP strtotime function.
The problem with strtotime is that it assumes you are using m/d/Y. If you want d/m/Y - you need to change it to d-m-Y to be correctly parsed.
So change your date format to d-m-Y and it will work.

Date comparison in Rails 3.2

Stage:
I have this model:
Promo(id: integer, start_date: datetime, end_date: datetime)
I want to know which current promotions.
May be our query should be like:
SELECT * FROM promos WHERE now BETWEEN start_date AND end_date;
Question:
How should I make it in Ruby?
Which is the correct way?
Thank you.
Rails is intelligent. If you retrieve a date/time, it will converted to DateTime object.
On contrary, passing DateTime object to the where clause (for Rails 3.x), it will correctly build the where clause.
String passed to where clause will be passed to the SQL where clause. If you pass an array,
second and later elements will be replaced with '?' character in the first element.
So, for your case:
Promo.where(["? between start_date and end_date", DateTime.now])
works. I verified on my Rails model(Position) which happen to have start_date and end_date, and works correctly:
[1] pry(main)> Position.where(["? between start_date and end_date", DateTime.now]).count
=> 2914
It's on Rails 3.2.8 with PostgreSQL.
Yes, you can use comparison operators to compare dates e.g.:
irb(main):018:0> yesterday = Date.new(2009,6,13)
=> #<Date: 4909991/2,0,2299161>
irb(main):019:0> Date.today > yesterday
=> true
But are you trying to compare a date to a datetime?
If that's the case, you'll want to convert the datetime to a date then do the comparison.
or
Date.parse('2010-11-01') < Date.today
will return true if '2010-11-01' has already passed
I hope this helps.
Haven't tested this, but try something like:
currentTime = Time.now()
Promo.find(
:all,
:conditions => ['start_date < ? AND end_date > ?', currentTime, currentTime]
)
You might need to use the Date class if the Time class doesn't work

Resources