I have a column having datetime objects in my table. I am using datatables plugin.
Upon trying to sort the particular column, it gets sorted with errors. Like,for the following dates
Aug. 20, 2018, 11:16 a.m.
Aug. 2, 2018, 12:25 p.m.
Aug. 4, 2018, 3:03 p.m.
I get the sorted result as
Aug. 2, 2018, 12:25 p.m.
Aug. 20, 2018, 11:16 a.m.
Aug. 4, 2018, 3:03 p.m.
What should be done to correct this?
As mentioned in jquery official document , you can only sort data for which format is mentioned.
and to sort date or time, you can add following lines to sort the date or time :
$('#example').dataTable( {
columnDefs: [
{ type: 'date-uk', targets: 0 }
]
} );
0 is the index of column. you can modify that based on your column index.
And you can change type date-uk to any other which is mentioned in document, if you receive data in same format.
Related
As we all know, date parsing in Go has it's quirks*.
However, I have now come up against needing to parse a datetime string in CCYY-MM-DDThh:mm:ss[.sss...] to a valid date in Go.
This CCYY format is a format that seems to be ubiquitous in astronomy, essentially the CC is the current century, so although we're in 2022, the century is the 21st century, meaning the date in CCYY format would be 2122.
How do I parse a date string in this format, when we can't specify a coded layout?
Should I just parse in that format, and subtract one "century" e.g., 2106 becomes 2006 in the parsed datetime...?
Has anyone come up against this niche problem before?
*(I for one would never have been able to remember January 2nd, 3:04:05 PM of 2006, UTC-0700 if it wasn't the exact time of my birth! I got lucky)
The time package does not support parsing centuries. You have to handle it yourself.
Also note that a simple subtraction is not enough, as e.g. the 21st century takes place between January 1, 2001 and December 31, 2100 (the year may start with 20 or 21). If the year ends with 00, you do not have to subtract 100 years.
I would write a helper function to parse such dates:
func parse(s string) (t time.Time, err error) {
t, err = time.Parse("2006-01-02T15:04:05[.000]", s)
if err == nil && t.Year()%100 != 0 {
t = t.AddDate(-100, 0, 0)
}
return
}
Testing it:
fmt.Println(parse("2101-12-31T12:13:14[.123]"))
fmt.Println(parse("2122-10-29T12:13:14[.123]"))
fmt.Println(parse("2100-12-31T12:13:14[.123]"))
fmt.Println(parse("2201-12-31T12:13:14[.123]"))
Which outputs (try it on the Go Playground):
2001-12-31 12:13:14.123 +0000 UTC <nil>
2022-10-29 12:13:14.123 +0000 UTC <nil>
2100-12-31 12:13:14.123 +0000 UTC <nil>
2101-12-31 12:13:14.123 +0000 UTC <nil>
As for remembering the layout's time:
January 2, 15:04:05, 2006 (zone: -0700) is a common order in the US, and in this representation parts are in increasing numerical order: January is month 1, 15 hour is 3PM, year 2006 is 6. So the ordinals are 1, 2, 3, 4, 5, 6, 7.
I for one would never have been able to remember January 2nd, 3:04:05 PM of 2006, UTC-0700 if it wasn't the exact time of my birth! I got lucky.
The reason for the Go time package layout is that it is derived from the Unix (and Unix-like) date command format. For example, on Linux,
$ date
Fri Apr 15 08:20:43 AM EDT 2022
$
Now, count from left to right,
Month = 1
Day = 2
Hour = 3 (or 15 = 12 + 3)
Minute = 4
Second = 5
Year = 6
Note: Rob Pike is an author of The Unix Programming Environment
Here is the date/time string I am passing into MomentJS
let value = '2018-07-21T17:09:51.000Z'
This should be 07/21/2018 # 5:09:51 PM
moment(value).format("dddd, MMMM Do YYYY, h:mm:ss a")
Moment returns: Saturday, July 21st 2018, 10:09:51 am
Why is it showing 10:09:51 am and not 5:09:51 PM ?
Thanks
Simple answer... as #jshamble indicated... make sure you are using the timzone method .utc()
Solution to my question in the example:
moment(value).utc().format("dddd, MMMM Do YYYY, h:mm:ss a")
I have several rows in my table like below:
row1: abc changed on 12 November, 2008 11:30 AM and its abc..region1
row2: defg updated 14 January, 2012 08:20 PM ......region2
row3: ghijkl corrected by 18 august, 2013 9:30 AM ..something..region3
My requirement is as follows:
All the above dates are in EST time zone and date format is exactly as above and does not change.
I want to update the dates in these rows from EST to different time zones as per the region in that row, and the format should be changed to something like 12 dec 2016 7:30 AM.
So the query I framed is (taking row1 as example) as below:
select regexp_replace(
'abc changed on 12 November, 2008 11:30 AM and its abc..region1',
'([0-9]{2})([[:blank:]]) (January|February|March|April|May|June|July|August|September|October|November|December)(,[[:blank:]])([0-9]{4})([[:blank:]])([0-9]{2}:[0-9]{2})([[:blank:]])(AM|PM)','\1-\3-\5 \7 \9',1,0,'i')
output:
abc changed on 12-November-2008 11:30 AM and its abc..region1
So I am happy with the above query till now because I get a string
with the formatted date. Even though this is not the final date
format, I can use this date to pass to some function which converts
this date according to the region do some processing and fianlly
return a date type.For the same purpose I add to_date in the above
query:
select regexp_replace(
'abc changed on 12 November, 2008 11:30 AM and its abc..region1',
'([0-9]{2})([[:blank:]]) (January|February|March|April|May|June|July|August|September|October|November|December)(,[[:blank:]])([0-9]{4})([[:blank:]])([0-9]{2}:[0-9]{2})([[:blank:]])(AM|PM)',
substr('\1-\3-\5 \7 \9',1),
1,0,'i')
output:
abc changed on 12-November-2008 11:30 AM and its
abc..region1 --> works fine till here
Now I am adding to_date to convert the date string type to real date
type to do some processing on it:
select regexp_replace(
'abc changed on 12 November, 2008 11:30 AM and its abc..region1',
'([0-9]{2})([[:blank:]]) (January|February|March|April|May|June|July|August|September|October|November|December)(,[[:blank:]])([0-9]{4})([[:blank:]])([0-9]{2}:[0-9]{2})([[:blank:]])(AM|PM)',
to_date(substr('\1-\3-\5 \7 \9',1),'dd-mon-yyyy HH:MI AM'),
1,0,'i')
This query is giving me an error:
ORA-01858: a non-numeric character found where a numeric was expected
I checked whether wrong parameters were being passed to
to_date(), and fired the query below, but it worked fine.
Select to_date('12-November-2008 11:30 AM','dd-mon-yyyy HH:MI AM')
from dual;
output:
12-Nov-2008
(I am not worried with the timestamp because itnternall it will be anyways in this date)
To avoid confusion I have numbered the substrings of the regular expression above:
([0-9]{2})-->1 ([[:blank:]])-->2
(January|February|March|April|May|June|July|August|September|October|November|December)-->3
(,[[:blank:]])-->4 ([0-9]{4})-->5 ([[:blank:]])-->6
([0-9]{2}:[0-9]{2})-->7 ([[:blank:]])-->8 (AM|PM)-->9
select regexp_replace(
'abc changed on 12 November, 2008 11:30 AM and its abc..region1',
'([0-9]{2})([[:blank:]]) (January|February|March|April|May|June|July|August|September|October|November|December)(,[[:blank:]])([0-9]{4})([[:blank:]])([0-9]{2}:[0-9]
{2})([[:blank:]])(AM|PM)','\1-\3-\5 \7 \9',1,0,'i')
Assuming your string always has the date in it in that particular format (and that there are no invalid dates etc etc) then the following should work for you:
WITH sample_data AS (SELECT ' the date is 12 November, 2008 11:30 AM' str FROM dual UNION ALL
SELECT 'Here''s a date of 1 March, 2015 1:43 pm' str FROM dual UNION ALL
SELECT '1 February,2016 9:43 AM' str FROM dual UNION ALL
SELECT 'And again it''s 21 May, 2016 9:43 AM and a little bit extra' str FROM dual)
SELECT str,
to_date(regexp_replace(str, '^.*?([[:digit:]]{1,2} [[:alpha:]]{3,9}, ?[[:digit:]]{4} [[:digit:]]{1,2}\:[[:digit:]]{2} (A|P)M).*$', '\1', 1, 1, 'i'), 'dd Month yyyy, hh:mi am') dt
FROM sample_data;
STR DT
---------------------------------------------------------- -------------------
the date is 12 November, 2008 11:30 AM 12/11/2008 11:30:00
Here's a date of 1 March, 2015 1:43 pm 01/03/2015 13:43:00
1 February,2016 9:43 AM 01/02/2016 09:43:00
And again it's 21 May, 2016 9:43 AM and a little bit extra 21/05/2016 09:43:00
The regular expression can be broken down as follows:
^.*? - match any character (except new line) from the start of the line as few times as possible, which may be 0 or more.
([[:digit:]]{1,2} [[:alpha:]]{3,9}, ?[[:digit:]]{4} [[:digit:]]{1,2}\:[[:digit:]]{2} (A|P)M) - this is the pattern we're looking for, and which we'll use to replace the whole string with (this is aliased as \1, which we can then pass into the replace string parameter).
.*$ - match any character up to the end of the string
The second part of the pattern can be further broken down as:
[[:digit:]]{1,2} - one or two digits
- a single space character
[[:alpha:]]{3,9} - three to nine letters (upper or lower case)
, ? - a comma followed by 0 or 1 spaces
[[:digit:]]{4} - four digits
- a single space character
[[:digit:]]{1,2} - one or two digits
\: - a single colon character
[[:digit:]]{1,2} - two digits
- a single space character
(A|P)M - either the letter A or P followed by an M
This should do the trick for you:
WITH sample_data AS (SELECT 'abc changed on 12 November, 2008 11:30 AM and its abc..region1' str FROM dual UNION ALL
SELECT 'defg updated 14 January, 2012 08:20 PM ......region2' str FROM dual UNION ALL
SELECT 'ghijkl corrected by 18 august, 2013 9:30 AM ..something..region3' str FROM dual)
SELECT str,
regexp_replace(str,
'(^.*?)(([[:digit:]]{1,2}) (January|February|March|April|May|June|July|August|September|October|November|December), (?[[:digit:]]{4} [[:digit:]]{1,2}\:[[:digit:]]{2} (A|P)M))(.*$)',
'\1\3-\4-\5\7', 1, 1, 'i') dt
FROM sample_data;
STR DT
------------------------------------------------------------------- --------------------------------------------------------------------------------
abc changed on 12 November, 2008 11:30 AM and its abc..region1 abc changed on 12-November-2008 11:30 AM and its abc..region1
defg updated 14 January, 2012 08:20 PM ......region2 defg updated 14-January-2012 08:20 PM ......region2
ghijkl corrected by 18 august, 2013 9:30 AM ..something..region3 ghijkl corrected by 18-august-2013 9:30 AM ..something..region3
I am trying to create a graph containing a date histogram. Everything is working except when crossing the daylight savings period. Check the table below for an example, notice the problem with the line of October 31st. Does anyone know a way to overcome this problem and get buckets beginning on each first of the month. The inputs are dates without a time, so for every record the time is midnight.
April 1st 2013, 00:00:00.000 11499
May 1st 2013, 00:00:00.000 11850
June 1st 2013, 00:00:00.000 15992
July 1st 2013, 00:00:00.000 14215
August 1st 2013, 00:00:00.000 14178
September 1st 2013, 00:00:00.000 12739
October 1st 2013, 00:00:00.000 14133
October 31st 2013, 23:00:00.000 13653
November 30th 2013, 23:00:00.000 12537
December 31st 2013, 23:00:00.000 14040
Just to make things better to debug, this is the executed query:
{
"size": 0,
"query": {
"query_string": {
"query": "*",
"analyze_wildcard": true
}
},
"aggs": {
"2": {
"date_histogram": {
"field": "orderCreationDate",
"interval": "1M",
"pre_zone": "+02:00",
"pre_zone_adjust_large_interval": true,
"min_doc_count": 1
}
}
}
}
The month format specifier doesn't seem to work.
from datetime import datetime
endDate = datetime.strptime('10 3 2011', '%j %m %Y')
print endDate
2011-01-10 00:00:00
endDate = datetime.strptime('21 5 1987', '%j %m %Y')
print endDate
1987-01-21 00:00:00
Now, according to the manual the manual:
%m = Month as a decimal number [01,12].
So, what am I missing, other than the hair I've pulled out trying to understand why my django __filter queries return nothing (the dates going in aren't valid!)? I've tried 03 and 05 to no avail.
Versions of things, platform, architecture et al:
$ python --version
Python 2.7
$ python3 --version
Python 3.1.2
$ uname -r
2.6.35.11-83.fc14.x86_64 (that's Linux/Fedora 14/64-bit).
You can't mix the %j with others format code like %m because if you look in the table that you linked %j is the Day of the year as a decimal number [001,366] so 10 correspondent to the 10 day of the year so it's 01 of January ...
So you have just to write :
>>> datetime.strptime('10 2011', '%j %Y')
datetime.datetime(2011, 1, 10, 0, 0)
Else if you you wanted to use 10 as the day of the mount you should do :
>>> datetime.strptime('10 3 2011', '%d %m %Y')
datetime.datetime(2011, 3, 10, 0, 0)
Isn't %j the "day of year" parser, which may be forcing strptime to choose January 21, overriding the %m rule?
%j specifies a day of the year. It's impossible for the 10th day of the year, January 10, to occur in March, so your month specification is being ignored. Garbage In, Garbage Out.