Using the Ci date helper, how would I turn '06/08/2012 17:10pm' into 12 hour format?
date('m/d/Y H:ia ', strtotime($row->entryCreationDateTime))
That code has nothing to do with CodeIgniter, it's simply PHP functions. For information on the date function, see the manual page.
The small chatacter h puts the hour in 12-hour format and capital chatacter H puts the hour in 24-hour format.
Change the code to this:
date('m/d/Y h:ia ', strtotime($row->entryCreationDateTime))
Related
Earlier someone answered my question on Python's datetime function which I am grateful to. However my followup comment/ question remains unanswered. Please help.
My followup question:
Thank you so much. Your answer helped. It works. However when you give 2 digit year with %y and you try to print the year with date.year, it prints prefix of 20xx (e.g. 2023 in my example below). If I wanted it to print 19xx what should I do?
Original question:
datetime.strptime not working as portrayed widely. Using Python 3.11
I see several posts talking about datetime.datetime.strptime that can be used to convert date string '12/13/23' to a datetime object. This doesn't work for me.
import datetime dt = datetime.datetime.strptime('12/13/2023', "%m/%d/%y") produces error as follows
Traceback (most recent call last): File "C:\Users\User\IdeaProjects\FirstTest\Scratchpad.py", line 3, in dt = datetime.datetime.strptime('12/13/2023', "%m/%d/%y") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib_strptime.py", line 568, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib_strptime.py", line 352, in _strptime raise ValueError("unconverted data remains: %s" % ValueError: unconverted data remains: 64
Many people show the above code with output working as 2023-12-13 as a datetime object. Doesn't work for me
I was expecting dt.month() to return the month and print(dt) to print yyyy-mm-dd
Answer:
Your code doesn't show the same code that you're running as your traceback.
Your code, which works:
dt = datetime.datetime.strptime('12/13/23', "%m/%d/%y")
Your traceback, which doesn't work:
dt = datetime.datetime.strptime('12/13/2023', "%m/%d/%y")
The traceback is correct to produce an error here, the format string you've given is not valid for the date string you've given. %y parse 2-digit year, and 2023 is not a 2-digit year, you need %Y if you want to parse a 4-digit year.
I have one requirement. in one of the source file I'm getting calendar date as input and while processing the file it has to convert Julian Date format. I just need a Script.
ex:Date: 10-Nov-2020
Julian Date: 2020314
The easiest way is
date -d 10-Nov-2020 +%Y%j
but it seems to count from 1, not 0, so it returns 2020315.
Perl's Time::Piece can be used to get the expected value:
perl -MTime::Piece -lwe '$t = localtime->strptime(shift, "%d-%b-%Y"); print $t->year, $t->yday' -- 10-Nov-2020
You might need sprintf "%03d", $t->yday instead of just $t->yday if you want 2020000 instead of 20200 for the first day.
I am attempting to change an SQL datetime variable (2016-06-09 14:29:34) into a format that looks like this (00:00_20160601). I have tried to follow a couple of SO questions that will allow me to format a Time object.
This is what I have done so far:
start_datetime = "2016-06-09 14:29:34"
t =Time.new(start_datetime)
t.strftime("%H:%M_%Y%d%m")
This results in the time being formatted to 2016-01-01 00:00:00 +0000, which is obviously not what I want. I was wondering if someone could help me format the datetime object the way I specified?
You can do this with DateTime:
require 'datetime'
DateTime.parse("2016-06-09 14:29:34").strftime("%H:%M_%Y%d%m")
#=> "14:29_20160906"
The format you're feeding in is basically ISO-8601 so it's parsed by default.
Feeding that value into Time.new is completely incorrect. The first argument there is the year, the rest have to be supplied separately. That's why you get 2016-01-01, since everything else comes out as defaults.
Time.new is converting automatically and the result of "2016-06-09 14:29:34".to_i is 2016.
It's not entirely clear why your day value changes from 09 in the input to 01 in the desired output, so I'll use the normal thing and output the same as was input:
require 'time'
start_datetime = "2016-06-09 14:29:34"
t = Time.strptime(start_datetime, '%Y-%m-%d %H:%M:%S')
t.strftime('00:00_%Y%m%d') # => "00:00_20160609"
Since the hours and minutes are being thrown away there are a couple of other ways to go about this.
Ignore the hours and minutes when parsing:
t = Time.strptime(start_datetime, '%Y-%m-%d')
Or use a Date object instead of a Time object:
require 'date'
start_datetime = "2016-06-09 14:29:34"
t = Date.strptime(start_datetime, '%Y-%m-%d')
t.strftime('00:00_%Y%m%d') # => "00:00_20160609"
Making a GET request to a private (no public documentation) API returns data in JSON format.
The value for date looks as follows:
AanmeldDatum: "/Date(1262300400000+0100)/"
There's another variable called AangebodenSindsTekst which means OfferedSinceText and it's value is "8 augustus 2014". So the unknown Date format should get parsed into that specific value.
I'm wondering what kind of date format it is and how can I transform this to something like this 2014-08-08 with Ruby?
I've tried this:
require 'time'
t = '1262300400000+0100'
t2 = Time.parse(t)
# => ArgumentError: no time information in "1262300400000+0100"
Ruby's Time class is your friend, especially the strptime method:
require 'time'
foo = Time.strptime('1262300400000+0100', '%N') # => 2014-08-08 16:57:25 -0700
foo = Time.strptime('1262300400000+0100', '%N%z') # => 2014-08-08 08:57:25 -0700
%N tells Ruby to use nanoseconds. It's throwing away the precision after the 9th digit which is OK if you don't need the rest of the value. Nanosecond accuracy is good enough for most of us.
%z tells Ruby to find the timezone offset, which it then applies to the returned value.
While parse can often figure out how to tear apart an incoming string, it's not bullet-proof, nor is it all-knowing. For speed, I'd recommend learning and relying on strptime if your strings are consistent.
As the Tin Man pointed out in this answer, use the following instead:
Time.strptime('1262300400000+0100', '%Q%z')
it could be milliseconds since epoc, take off the last 3 zeros and plug it into a unix time stamp converter, comes out as Dec 31st 2009
TIME STAMP: 1262300400
DATE (M/D/Y # h:m:s): 12 / 31 / 09 # 11:00:00pm UTC
I'm pulling date-time strings from a large CSV file, which look like this:
"11/19/2008 21:56"
I'd like to extract the hour only, so I can build a histogram of all the hours to find the most frequent ones. Similarly, I'd like to extract days of the week (names) from the dates and build a histogram of most frequent days.
I'm new to Ruby, looked up the information, for starters tried various forms of the following, but no luck:
require 'date'
puts DateTime.strptime("11/19/2008 21:56", '%I')
Can you please advise a simple (and clear) way to accomplish the above? Also, any suggestions how to represent the results would be great. I'm thinking one hash array for the hours (24 entries) and one for the days (7 entries)? What would be the neatest algorithm to load them up as I iterate through the date-time strings, and then maybe re-sorting them with most frequent on top? Thanks!!
This is the starting point:
dt = "11/19/2008 21:56"
require 'date'
DateTime.strptime(dt, '%m/%d/%Y %H:%M') # => #<DateTime: 2008-11-19T21:56:00+00:00 ((2454790j,78960s,0n),+0s,2299161j)>
Date formats like "11/19/2008" present a problem when parsing because the default is to use this format:
'%d/%m/%Y'
Date blows up when it sees a month value of 19. '%m/%d/%Y' is not as popular around the world as '%d/%m/%Y', which is why Ruby defaults to it.
Once you have the timestamp parsed, you can easily extract parts from it:
datetime = DateTime.strptime(dt, '%m/%d/%Y %H:%M')
datetime.hour # => 21
datetime.wday # => 3
Notice that wday returns values from 0..6, not 1..7, where 0 = Sunday:
%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday][datetime.wday]
# => "Wednesday"
Rails' ActiveSupport has a lot of useful methods as part of its Date, DateTime and Time support. Using them is easy, and it's easy to cherry-pick which you want if you decide to add them to plain-ol' Ruby code.
"11/19/2008 21:56".split[1]
=> "21:56"
If can be in other formats, but always the only part with a ":" and two digits on each side, you can use
"11/19/2008 21:56"[/\d{2}:\d{2}/]
=> "21:56"
And for day, something similar
"11/19/2008 21:56"[/\d{2}\/\d{2}\/\d{4}/]
=> "11/19/2008"