So I am iterating through a hash where one of the key/values is {date: => 'MM/DD/YYYY'}
When I iterate through, I am using the date gem to find out what day of the week that each date is, (0-6).
To get a day of the week for the index I am currently at as an integer so i can compare it to another integer, the idea is to check if the day of the week of the index is the same as the day of the week i am searching for.
To get that int I run the following commands:
d = Date.parse(hash[i].values[2])
day_of_the_week = d.cwday
When i do this on its own for just a cherry-picked date this works fine, but I am iterating through the hash, what i get is:
search.rb:25:in `parse': invalid date (ArgumentError)
for the particular date '9/13/17'.
Is there something wrong with '9/13/17'? Why does this actually work for other days (it starts at '9/5/17') and then get randomly stuck at this day?
And as I was writing this, I did a little digging and found exactly what index it was:
d = Date.parse(hash[4224].values[2])
day_of_the_week = d.cwday
Gives me the same error, I am completely baffled, what is going on? Also its not the lack of MM in 9/etc because every other month is the same way.
EDIT: The result should be 2, September 12th 2017 was a Tuesday.
You need to pass the format of your date, use
Date.strptime('9/13/2017', '%m/%e/%Y').
I found that using:
d = Date.strptime(hash[i].values[2], '%m/%d/%Y')
Does creates a date object of the current index better than:
d = Date.parse(hash[i].values[2].to_s)
Replacing that did the trick.
Related
I have a method that returns date in the format I want to have it. I work with calendar weeks, so I don't save them as dates and convert them in the method to display them nicely in the view (when necessary).
Anyway, this is the method:
def formatted_date(year, week, day)
Date.strptime("#{year}-#{week}-#{day}", '%Y-%V-%u')
end
When type in formatted_date(2022, 47, 21) it returns the date for today, but when I type in formatted_date(2023, 47, 21) it also returns the day for today, not for 2023.
Why is that? And how do i do it correctly? I thought it just returns the year with four digits which is the case here, right?
You are passing the wrong format to strptime which is why the year isn't changing. Do something like this:
def formatted_date(date)
DateTime.strptime(date, "%d-%m-%Y").strftime("%Y-%m-%W")
end
Pass the date format like this formatted_date("21-02-2022") and you will get the result in the format you are looking for. Obviously, you can change strftime("%Y-%m-%W") the way you want to.
Hello i've been working on how to get yesterday date in DataStage?
CurrentDate()-1
When i compile the job, it gave me an error.
So how should i do to get the yesterday date?
btw that code i'm doing it in the Transformer stage
Assuming you are using the parallel engine in DataStage - this could be a solution
DateOffsetByComponents
DateOffsetByComponents(CurrentDate(), 0, 0, -1)
As the last parameter is the day part and -1 would substract a day
Convert the date into a date type, then you can add or subtract days.
You can use IConv to convert a string into a datastage internal date format. Then you can perform addition/subtraction on the date. Then use OConv to convert the variable back to string format.
If this is done in a transformer stage, you need to do this all in one statement:
OConv(Iconv(VDate ,"D/YMD[4,2,2]") - 1), "D/YMD[4,2,2]")
Hope this helps.
In a parallel Transformer stage, I'd use DateFromDaysSince() function. Use current date function as the base, and -1 as the offset value.
I need to get time and date from database date like "2015-08-27T12:09:36Z". I tried but not get any solutions where I get date and time in different variable.
I need to get it in Ruby. No rails in my application.
I used below code but not getting.
Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z").in_time_zone
Any one have a experience in it?
Thanks
I don't have enough reputations to comment so am posting comment as answer, are you looking for this
Time.now.strftime('%Y-%m-%dT%H:%M:%SZ')
Which will give the pattern you asked for. Z represent the time zone if you use
%z - Time zone as hour and minute offset from UTC (e.g. +0900)
%:z - hour and minute offset from UTC with a colon (e.g. +09:00)
%::z - hour, minute and second offset from UTC (e.g. +09:00:00)
%Z - Time zone abbreviation name
Check for more
http://apidock.com/ruby/Time/strftime
My Updated answer after your comment
require 'date'
DateTime.parse("2015-08-27T12:09:36Z").strftime('%Y-%m-%dT%H:%M:%SZ')
In your code change Time.strptime('') to DateTime.strptime('')
First, you need to require 'date'. Ruby has built-in Date and Time classes without that require, but the library provides more functionality.
If you have a string retrieved from the database in ISO-8601 format, and you want to turn it into a date and time, just use DateTime.iso8601(string) to get a DateTime object. You can extract the date and time components however you like after that.
irb(main):001:0> require 'date' #=> true
irb(main):002:0> dt = DateTime.iso8601("2015-08-27T12:09:36Z") # DateTime object
irb(main):003:0> d = dt.to_date # Date object
irb(main):004:0> t = dt.to_time # Time object
Using Ruby 2.1, I am trying to find the reciprocal of Time#strftime('%Y%U'). For example:
s = Time.parse("2014-05-07 16:41:48 -0700").strftime('%Y%U')
# 201418
t = Time.strptime(s, '%Y%U')
# Expected: 2014-05-04 00:00:00 -0700
# Actual: 2014-01-01 00:00:00 -0800
This topic suggested to use %G so I read the docs and tried it, but all I get out of it is the current Time. eg:
t = Time.strptime('201418', '%G%U')
# 2014-05-13 12:07:51 -0700
From the docs, it looks to me that %G is only intended to work with %V as both are ISO 8601 and %U is not, but even even using %G%V I get back the current time.
So what's the right way to turn a %Y%U string into the corresponding Time?
This could be a bug, since it seems to work fine if you use DateTime instead.
s = Time.parse("2014-05-07 16:41:48 -0700").strftime('%Y%U')
DateTime.strptime(s, "%Y%U")
#<DateTime: 2014-05-04T00:00:00+00:00 ((2456782j,0s,0n),+0s,2299161j)>
Edit:
If you look at the source code this is actually just dumb coding. Time.strptime calls Date._strptime which correctly parses out the year and week number. But then Time.strptime stupidly only looks for the usual year, month, day, hour, etc. and ignores the week number entirely.
I submitted a bug report.
There are at least three types which represent time: Time, Date and DateTime(from ActiveSupport).
My problem is, could DateTime totally replace Date? In other words, if I can use DateTime, is there any reason to use Date instead?
require 'date'
d = Date.today
dt = DateTime.now
p Date.public_methods - DateTime.public_methods
#=>[:today]
p DateTime.public_methods - Date.public_methods
#=>[:now]
p d.public_methods - dt.public_methods
#=>[]
p dt.public_methods - d.public_methods
#=>[:hour, :min, :minute, :sec, :second, :sec_fraction, :second_fraction, :offset, :zone, :new_offset]
DateTime is a subclass of Date. Using DateTime, you lose the today Class method and get now in return. You don't lose instance methods.
If you want to store only the date, for example a birthday, or a certain day where an event takes place, then it can be easier to use only date. Then you have no troubles which arise from different time zones and time zone calculations. If you use DateTime, then if you add an offset of -2 hours to 00:00 am, you get 10:00 pm of the previous day.
Date does not store any information about the time, neither with the timezone. So you might get into trouble if at some point you'll need to use time data.
Cf this link, which I found clear about what classes should be used, when, and how.