Date error in DateProperty() save via ndb - google-app-engine

Strange results on a saved date in GAE (Python with ndb library).
The inbound string from a web form is %a %m/%d/%y formatted (Fri 3/3/17).
That's parsed with datetime.strptime to get a date value.
When it's saved in a DateProperty() field, the value is consistently the day before, at 16:00:00.000 PST.
postDate = datetime.datetime.strptime(self.request.get('date-'+
hashed_id),'%a %m/%d/%y')
logging.info('postDate as Date: %s',postDate)
postDateStr = datetime.datetime.strftime(postDate,'%D')
logging.info('postDateStr: %s',postDateStr)
thisPost = ScheduledPost(id = postID,
...
postDate = postDate,
postDateStr = postDateStr
)
log results:
postDate as Date: 2017-03-03 00:00:00
postDateStr: 03/03/17
so far so good, right? but in the Datastore interface, that record shows:
PostDate: 2017-03-02 (16:00:00:000 PST)
PostDateStr: 03/03/17
Oops.
Workstation is in Pacific time - but leaving that aside - date queries seem to confirm that the date is wrong. Assuming today is 3/3/17 -
today = dt.datetime.now()
ScheduledPost.query(ScheduledPost.postDate == today).fetch()
No record returned.
Saving date as string, and querying on date as string, are feasible workarounds for this project. Just thought it merited posting - anyone seen this? Advice?

The datetime you're generating on appengine is UTC, and it gets stored as that datetime, without timezone information.
When you're viewing it, it's being converted to pacific-time.
The query you're doing is incorrect: you're generating a datetime, not a date, so the time you're comparing with is going to be different.

Related

How to get formatted DateTime irrespective of the time zone in salesforce

We use formatGMT() or format() methods of date time object in apex, it returns the formatted date based on the time zone. Although what should be done if we have a static date, such as date of birth that needs to be formatted, irrespective of the local time zone.
In my scenario when using DateTime.format(), The date of birth is showing correctly for users in India, although for the users in US, yesterday's date is displayed in some cases. In some cases it's converted correctly.
For eg. The dob for x person is 1995-09-20 which gets formatted to 09/20/1995,
Whereas for y person, the same method generates a formatted date 05/15/1990 when dob is 1990-05-16.
Basically the formatted date should be the exact replica of the dob, irrespective of time zone.
Method:
private static String localeFormatDate (Date dateValue) {
try{
String format = String.isNotBlank (strDateFormat) ? strDateFormat: 'MM/dd/YYYY';
Date myDate = Date.newInstance (dateValue.year(), dateValue.month(), dateValue.day());
Time myTime = Time.newInstance(0, 0, 0, 0);
return DateTime.newInstanceGMT (myDate, myTime). format GMT (format);
} catch (Exception ex){
return null;
}
}
Additionally, I have also tried to set timezone as Asia/Colombo as the date is correctly formatted in that time zone:
return DateTime.newInstanceGMT(myDate, myTime).format (format, 'Asia/Colombo');
Although I'm not sure if that would generate the correct format date based on the when the method runs.

How to check if a list of dates contains today's date in Lua?

I want to write a code that automatically checks if today's date matches one of the dates in a given table. I'm not very familiar with Lua(still learning) but I do know how to use the time function howevers the platform I'm working on only support Lua 5.0 so it does not support the yday(%j) function which makes it more complicated.
You can try formatting your date using this code:
-- CURRENT DATE
local myyear = os.date("*t").year
local mymonth = os.date("*t").month; if mymonth < 10 then mymonth = "0"..mymonth end
local myday = os.date("*t").day; if myday < 10 then myday = "0"..myday end
local today = myyear..mymonth..myday
print(today)
And then check if the date is in your table.
Hope this is not too complicated and can be of help?
PS: I am using gideros engine which uses lua.

How do I Check for valid date in C# (that has a datetime type not datetime2)?

I've been searching but haven't found my answer so forgive me if this question is a duplicate.
I've got a .Net C# application that is using entity framework (EF) to communicate with a SQL Server database. I'm converting a large amount of data and I need to make sure my dates are valid SQL Server datetime types. My POCO classes use a datetime2 type for the dates so a date '0201-04-11 13:00:00 PM' is valid until the insert is actually attempted in the SQL Server database. I was attempting to use DateTime.TryParseExact with something like this...
if (DateTime.TryParseExact(legacyRecord.date_paid.ToString(), "M/d/yyyy hh:mm:ss tt", new CultureInfo("en-us"), DateTimeStyles.None, out datePaid))
{
// Load record into lease payment table table
LoadLeasePayment loadLeasePayment = new LoadLeasePayment();
Decimal LeasePaymentId = loadLeasePayment.AddRecord(prodLeaseId, legacyRecord.amount_paid, datePaid, prodContext, loadDate);
}
I'm sure the solution is obvious but I cannot see the forest for the trees. Any help is much appreciated.
After parsing the string DateTime value, you'll need to verify it is within the range of the target SQL data type. The SqlDateTime structure includes static MinValue and MaxValue fields to facilitate this.
if (DateTime.TryParseExact(legacyRecord.date_paid.ToString(), "M/d/yyyy hh:mm:ss tt", new CultureInfo("en-us"), DateTimeStyles.None, out datePaid))
{
if((datePaid >= SqlDateTime.MinValue) && (datePaid <= SqlDateTime.MaxValue))
{
// Load record into lease payment table table
LoadLeasePayment loadLeasePayment = new LoadLeasePayment();
Decimal LeasePaymentId = loadLeasePayment.AddRecord(prodLeaseId, legacyRecord.amount_paid, datePaid, prodContext, loadDate);
}
}

Sybase dataadd time where time added is the difference of 2 times

I have a sybase database and would like to create a new bigdatetimefield by adding time to a current bigdatetimefield
for example
I have a date1 field = 8/31/2015 2:23:49.529000 PM
I have a date2 field = 8/31/2015 7:23:49.529000 AM
I have a mainDate field = 8/31/2015 2:24:46.112000 PM
I would like to make a new field that is the mainDate field minus the difference in time between the date1 field and the date2 field
So in this case the new filed would be 8/31/2015 2:24:46.112000 PM - (8/31/2015 2:23:49.529000 PM - 8/31/2015 7:23:49.529000 AM)
Any idea how to do that in sybase?
SELECT new_dt = DATEADD(ss, datediff(ss,date1,date2),mainDate)
FROM my_table
That's only accurate to seconds though. You can use millisecond or microsecond.

Assign date in salesforce.

I have written a batch job. In this batch job i have a condition to take date > = 1/1/2012
I am not able to give the date in the query. can you please help me.
global Database.QueryLocator start(Database.BatchableContext BC) {
system.debug('Inside start');
//date mydate = date.valueof(1/1/2012);
return Database.getQueryLocator('select name from opportunity');
}
I have given it in 2 ways
1st is :
took the date into a date field and give the condition as date >= :mydate
(shows an error in debug log as Invalid date: 0 )
and
2nd way when i gave the date their itself as date >=: 1/1/2012
(it shows as error in debug log as unexpected token: / )
can you please help me
Thanks
Anu
You must follow the proper date format
YYYY-MM-DD
select name from opportunity where mydate >= 2012-01-01
More information here

Resources