My objective is to count Lead records based on simple date range query in local time
Integer TotalLeads = [ Select Count() From Lead where createddate >= fromdate CreatedDate <= todate];
Fairly basic. However, the issue I'm running into is I only want to count the leads for the "local" time not UTC; createddate is in UTC for lead records.
Sample dates:
From: 03/23/2017
To: 03/29/2017
For these sample dates and my local time is UTC - 7 (Los Angeles), so my query would be
Integer TotalLeads = [ Select Count() From Lead where createddate >= 2017-03-23T07:00:00z AND CreatedDate <= 2017-03-30T06:59:59z];
If these are my dates, how do I append the local time so from date is 2017-03-23T07:00:00z and to date is 2017-03-30T06:59:59z?
Using from date first, I was able to do the following but can't figure how to keep it in local time
// Date
date ds = date.valueof('2017-03-23');
string dm = string.valueOf( ds.month());
string dd = string.valueOf(ds.day());
string dy = string.valueOf(ds.year());
// DateTime Midnight (UTC)
String SDate = string.valueof(dm +'/' + dd + '/' + dy + ' 12:00 AM');
system.debug(SDate); // -> 3/23/2017 12:00 AM
// DateTime (Local Time)
datetime ds2 = datetime.parse(SDate );
system.debug(ds2); // -> 2017-03-23 07:00:00
system.debug(ds2.format('yyyy-MM-dd\'T\'hh:mm:ss'')); // -> 2017-03-23T12:00:00
As you can see, using ds2.format put its in the format I need but back to UTC (midnight), I need it to be 2017-03-23T07:00:00
Any suggestions?
Thanks!
Figured what I was doing wrong. The date calculation was fine, it had to do with how this was being passed to a batch job.
Related
I have a datetime stored in SQL Server like '2021-10-12 18:46:31.047' and '2021-10-225 23:54:08.667' in one row..
How can I query from datetime by using date only not hours
WHERE str_day = '2021-10-23' AND end_day = '2021-10-24'
Help please
Use date boundaries:
WHERE str_day >= '20211023'
AND str_day < '20211024'
AND end_day >= '20211024'
AND end_day < '20211025'
If your columns str_day and end_day are of time DATETIME (or DATETIME2(n)), then you can use something like this:
WHERE CAST(str_day AS DATE) = '2021-10-23'
AND CAST(end_day AS DATE) = '2021-10-24'
DATE is the date-only (no time portion) datatype - if your time portion is never needed, it might be useful to store those things as DATE from the beginning...
Use this; this will work for many scenario and types
WHERE str_day = '23 OCT 2021'
AND end_day = '24 OCT 2021'
Hi I am using Salesforce Apex,
I have a date as String as below. I need to add days to it using Apex.
String dateTime = '2017-07-08T23:59:59Z';
If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?
Thanks!
Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.
To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:
DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:
DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);
You need to convert the string to a DateTime and then add days. You can format it back after
String stringDateTime = '2017-07-08T23:59:59Z';
DateTime dt = DateTime.valueOfGmt(stringDateTime);
DateTime tomorrow = dt.addDays(1);
DateTime nextMonth = dt.addMonths(1);
DateTime anniversary = dt.addYears(1);
String formattedDateTime = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
I am inserting into the table with field type "timestamp with time zone" string "1858-11-17 01:09:05+0000" and getting back strage formated value "05:11:29+04:02:24".
Here is session
test=> create table ddtbl (val timestamp with time zone);
CREATE TABLE
test=> insert into ddtbl (val) values ('1858-11-17 01:09:05+0000');
INSERT 0 1
test=> select * from ddtbl;
val
------------------------------
1858-11-17 05:11:29+04:02:24
Why is this happening and what is "+04:02:24" here ?
UPD: PostgreSQL version
% psql --version
psql (PostgreSQL) 9.2.4
UPD2: Local timezone
% date +%Z
YEKT
% date +%z
+0600
This is an effect of the time zone. Before early 20th century many countries (like Germany or Russia) had completely different regimes like "mean solar time" which would not translate cleanly to UTC.
Therefore a time in time zone 0 (GMT at the time, as there was no UTC yet) would have an odd time offset when represented as local time for Yekaterinburg (Russia).
+04:02:24 is the actual offset as compared to UTC.
It is interpreting your input value is UTC.
psql=# select cast('1858-11-17 01:09:05 UTC' as timestamp with time zone);
timestamptz
------------------------
1858-11-17 01:09:05+00
(1 row)
psql=# select cast('1858-11-17 01:09:05 BRT' as timestamp with time zone);
timestamptz
------------------------
1858-11-17 04:09:05+00
(1 row)
The two values are the just different representations of the same timestamp.
psql=# select cast('1858-11-17 05:11:29+04:02:24' as timestamp with time zone) = cast('1858-11-17 01:09:05+0000' as timestamp with time zone);
?column?
----------
t
(1 row)
Currently I'm using Code On Time to develop my system.
I got a default current date and time, after the user selects a completed date from lookup, I want to calculate how many days and hours have been taken by the user.
Is there any way to do this?
Is this what you're after?
declare #dateTimeNow datetime = getutcdate()
, #dateTimeThen datetime = '2012-11-28 12:00:00'
select case when DATEPART(hour,(#dateTimeNow - #dateTimeThen)) >0 then day(#dateTimeNow - #dateTimeThen)-1 else day(#dateTimeNow - #dateTimeThen) end days
, DATEPART(hour,(#dateTimeNow - #dateTimeThen)) hours
or
select DATEDIFF(day,#datetimethen, #datetimenow) - case when (DATEDIFF(Hour,#datetimethen, #datetimenow)) % 24 = 0 then 0 else 1 end days
, DATEDIFF(hour,#datetimethen, #datetimenow) % 24 hours
Is it possible to do a QuerySet filter that returns object form a specified, recurring time span?
For example, given a start and an end date a few days apart, I would like to be able to find:
Get all objects with timestamp between 2pm and 7pm for every day between start and end
Get all objects with timestamp between 1am and 8am for every day that is either a saturday or a sunday.
NOT TESTED
For the first case, assuming MyModel and your timestamp is called date you can do:
import datetime
start_date = datetime.date(2012,7,1)
end_date = datetime.date(2012,7,16)
start_time = datetime.time(14,0)
end_time = datetime.time(19,0)
date_range = end_date - start_date
range_list = None
for days in xrange(date_range.days):
d = start_date + datetime.timedelta(days)
if range_list is None:
range_list = Q(date__range = (datetime.datetime.combine(d, start_time), datetime.datetime.combine(d, end_time)))
else:
range_list = range_list|Q(date__range = (datetime.datetime.combine(d, start_time), datetime.datetime.combine(d, end_time)))
MyModel.objects.filter(range_list)