I have a date time item in a list that I want to interact with. I access the items in the list via SPListItem.
My code:
_relevantDate = (DateTime)_siteInfo["cimKeyDocumentDate"];
What the code means in terms of types etc:
[datetime property] = [datetime cast]SPListItem[field name];
I'm aware of the SPFieldDateTime type but I can't see a type it returns and I am confused at how I should do return the datetime.
Thanks.
Your first code would return datetime type after correct casting.
DateTime myDateTime = DateTime.Parse(myListItem["MyDateField"].ToString());
but if you want to use SPFieldDateTime then you will have to write it like
SPFieldDateTime myDateTime =(SPFieldDateTime)web.Fields[FieldNames.DateFieldName];
This will return a field that contains date and time values.See here.
Related
i tried to modify a collection before returning it to be exported with maatwebsite Excel. The column i tried to modify is the created_date column (the default created_date of laravel) so that when exported to excel, i only get the date not datetime.
i tried:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
$temp = Invoice::all();
$len = count($temp);
for($i=0;$i<$len;$i+=1){
$temp[$i]->created_at = $temp[$i]->created_at->format('m/d/Y')
}
return $temp;
}
}
in the controller:
public function export()
{
return Excel::download(new InvoicesExport, 'invoices.xlsx');
}
but, when exported, the result in the excel file is, for example: '07/26/2016T00:00:00.000000Z'
i noticed that the time become zero, and when i tried:
$temp[$i]->created_at = "some random string"
the laravel in webpage return error said that "some random string" cannot be parsed to datetime by Carbon Class constructor.
how can i make the exporting does not construct datetime with the string i give in the 'created_at' column, but just return the plain string instead? if let's say, i can't modify the database so i can't create extra column and extra column for this simple thing is too much, i think.
If you are using Maatwebsite\Excel 2.1 try column formatting by the setColumnFormat($array) method.
If Maatwebsite\Excel 3.1, try with the WithMapping interface.
created_at is marked as 'datetime' in the $cast property of your model by default so when you set it it's automatically cast as date (with Carbon::parse('the value you gave'))
If you want to export your collection with some values formatted you should not mutate the objects of the original objects but create new dedicated objects/arrays that will not interfere with model behavior nor corrupt your existing objects.
I want to get events of my calendar in a particular date using python O365 library.
#code to create account instance
schedule = account.schedule()
for item in schedule.get_events(query= "start/dateTime ge '2019-11-23T08:00' and end/dateTime ge '2019-11-23T19:00'",include_recurring=False):
print(item)
When I do this, I got all the events including the events not related to the date I specified.
I have gone through the docs and source code of O365 Calendar Query class, they are using 2 formats to get date value in query parameter.
1)Query instance - I tried but don't know how to pass value for this instance.
2)String instance - Above given code, but not getting the expected answer.
Please explain the format to pass Query instance and also point out why the above code is not extracts the exact date events?
To create and use a Query instance with a start and end time and date:
import datetime
...
start = datetime.datetime.combine(datetime.date(2019, 11, 23), datetime.time(8, 00))
end = datetime.datetime.combine(datetime.date(2021, 11, 23), datetime.time(19, 00))
query = calendar.new_query('start').greater_equal(start)
query.new('end').less_equal(end)
items = calendar.get_events(query=query, include_recurring=False)
for item in items:
print(item)
calendar.new_query creates a new query and query.new merges 2 queries together, in this case 'start' and 'end'.
I have document contains properties like this
{
"id":"1bd13f8f-b56a-48cb-9b49-7fc4d88beeac",
"name":"Sam",
"createdOnDateTime": "2018-07-23T12:47:42.6407069Z"
}
I want to query a document on basis of createdOnDateTime which is stored as string.
query e.g. -
SELECT * FROM c where c.createdOnDateTime>='2018-07-23' AND c.createdOnDateTime<='2018-07-23'
This will return all documents which are created on that day.
I am providing date value from date selector which gives only date without time so, it gives me problem while comparing date.
Is there any way to remove time from createdOnDateTime property or is there any other way to achieve this?
CosmosDB clients are storing timestamps in ISO8601 format and one of the good reasons to do so is that its lexicographical order matches the flow of time. Meaning - you can sort and compare those strings and get them ordered by time they represent.
So in this case you don't need to remove time components just modify the passed in parameters to get the result you need. If you want all entries from entire date of 2018-07-23 then you can use query:
SELECT * FROM c
WHERE c.createdOnDateTime >= '2018-07-23'
AND c.createdOnDateTime < '2018-07-24'
Please note that this query can use a RANGE index on createdOnDateTime.
Please use User Defined Function to implement your requirement, no need to update createdOnDateTime property.
UDF:
function con(date){
var myDate = new Date(date);
var month = myDate.getMonth()+1;
if(month<10){
month = "0"+month;
}
return myDate.getFullYear()+"-"+month+"-"+myDate.getDate();
}
SQL:
SELECT c.id,c.createdOnDateTime FROM c where udf.con(c.createdOnDateTime)>='2018-07-23' AND udf.con(c.createdOnDateTime)<='2018-07-23'
Output :
Hope it helps you.
I have the following query:
SELECT * FROM c
WHERE c.DateTime >= "2017-03-20T10:07:17.9894476+01:00" AND c.DateTime <= "2017-03-22T10:07:17.9904464+01:00"
ORDER BY c.DateTime DESC
So as you can see I have a WHERE condition for a property with the type DateTimeand I want to sort my result by the same one.
The query ends with the following error:
Order-by item requires a range index to be defined on the corresponding index path.
I have absolutely no idea what this error message is about :(
Has anybody any idea?
You can also do one thing that don't require indexing explicitly. Azure documentBD is providing indexing on numbers field by default so you can store the date in long format. Because you are already converting date to string, you can also convert date to long an store, then you can implement range query.
I think I found a possible solution, thanks for pointing out the issue with the index.
As stated in the following article https://learn.microsoft.com/en-us/azure/documentdb/documentdb-working-with-dates#indexing-datetimes-for-range-queries I changed the index for the datatype string to RangeIndex, to allow range queries:
DocumentCollection collection = new DocumentCollection { Id = "orders" };
collection.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
await client.CreateDocumentCollectionAsync("/dbs/orderdb", collection);
And it seems to work! If there are any undesired side effects I will let you know.
SPListItem.GetFormattedValue seems to have a strange behavior for DateTime fields.
It retrieves the DateTime value through SPListItem's indexer which according to this MSDN article returns local time.
Here's a snippet from Reflector
public string GetFormattedValue(string fieldName)
{
SPField field = this.Fields.GetField(fieldName);
if (field != null)
{
return field.GetFieldValueAsHtml(this[fieldName]);
}
return null;
}
So it uses SPListItem's indexer to retrieve the value and than SPFields.GetFieldValueAsHtml to format the value. GetFieldValueAsHtml seems to assume the date is in UTC and convert it to local time no matter what kind it is. (Reflector shows that it uses GetFieldValueAsText which uses value.ToString() but for some reason it assumes the time to be UTC.)
The end result is that the string representation on a time field obtained trough listItem.GetFormattedValue() (at least in my case) is incorrect, being local time + (local time - UTC).
Have anybody encountered the same issue with SPListItem.GetFormattedValue() and what was your workaround?
Converting the date back to universal time before calling GetFieldValueAsHtml works just fine.
DateTime localTime = (DateTime)item["DueDate"];
// this is local time but if you do localDateTime.Kind it returns Unspecified
// treats the date as universal time..
// let's give it the universal time :)
DateTime universalTime = SPContext.Current.Web
.RegionalSettings.TimeZone.LocalTimeToUTC(localTime);
string correctFormattedValue =
item.Fields["DueDate"].GetFieldValueAsHtml(universalTime);
I have had a recognised bug with the date conversion from UTC in SharePoint. It was fixed in SP1.