Getting month from date time

Hi all,

I’m trying to get month from two date time values so that I can compare them in a <<if[]>> condition.
Using <<[new DateTime().Month]>> I am able to get “9” to output.
But if I try <<[startdate]>><<[startdate.Month]>> it only outputs the following:
9/6/2023 2:00:00 PM –

Would appreciate any advice.

@airbags What data source do you use? I have tested with the following code and it works as expected:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<[date.Month]>>");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, DateTime.Now, "date");

doc.Save(@"C:\Temp\out.docx");

I’m using a JsonDataSource with the date time as below
{“startdate”: “2023-09-05T14:00:00Z”}

ReportingEngine engine = new ReportingEngine();
engine.Options |= ReportBuildOptions.RemoveEmptyParagraphs;
engine.Options |= ReportBuildOptions.AllowMissingMembers;
engine.Options |= ReportBuildOptions.InlineErrorMessages;
engine.KnownTypes.Add(typeof(DateTime));
engine.BuildReport(doc, jsonDataSource);

My colleague figure it out. He cast’ed the json property to a DateTime typed variable.

<<var[DateTime date1 = startdate]>> <<[date1.Month]>>
Output:
9

@airbags It is perfect that you managed to find a solution. Date value from JSON source is read as nullable DateTime? type. To use DateTime.Month property it is required to cast nullable DateTime? type to DateTime. For example see the following code:

string json = "{\"startdate\": \"2023-09-05T14:00:00Z\"}";
JsonDataSource jsonData = new JsonDataSource(new MemoryStream(Encoding.UTF8.GetBytes(json)));

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<[((DateTime)startdate).Month]>>");

ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(DateTime));
engine.BuildReport(doc, jsonData);

doc.Save(@"C:\Temp\out.docx");