[Reporting engine] Datetime formating in variable tag

Hello,

I’m using Aspose reporting engine, and I would like to defined a string variable from a datetime and apply it a specific date format (for example "dd/MM/yyyy").

This variable is not used to be displayed directly in the report, in this case I will use << [my_datetime]:dd/MM/yyyy">>. I just need it in my tempate for some process.

I tried folowing syntax, but it doesn’t work, it return an empty string:
<<var [string string_datetime_tostring = my_datetime.ToString("dd/MM/yyyy")]>>

Is it a way to do this kind of convertion in my var tag?

Thank for your help

@bgam @Apurv45 To be able to use format switches, the value should be in the correct type. In your particular case, you can either pass DateTime value or parse the passed string value into DateTime. For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("<<var[date = \"1/1/2020\"] >>");
builder.Writeln("<<[DateTime.Parse(date)]:\"dd/MM/yyyy\">>");

ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(DateTime)); // Register DateTime as a known type.
engine.BuildReport(doc, new object());

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

Note, DateTime should be registered as a known type.

@alexey.noskov thank you for your quick answer.

I’m not sure we understood each other well. My cas seems to be the opposite of your exemple:
In your case you convert string to date
In my cas in would like to convert date to string in a variable;

My case is:

  • I have a datetime as input value
  • I want to create a string variable using this datetime and format it as dd/MM/yyyy
  • the variable is a string

so is it a way to convert a datetime to string in a variable with specified format?

Than you

@bgam Thank you for additional information. The following syntax seems to work fin in such scenario:
<<var[date = mydate.ToString("dd/MM/yyyy")] >>
For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("<<var[date = mydate.ToString(\"dd/MM/yyyy\")] >>");
builder.Writeln("<<[date]>>");
builder.Write("Todays date is <<[date]>>");

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

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

@alexey.noskov : thanks.

It’s working using variable “mydate” as input, but t if myDate in a property of a custom object it doesnt work (myobject.mydate).

I enclosed my test solutionAsposeTest_date.zip (203.6 KB)

Could you check engine behaviour in this context?

Thank you

@bgam Your objects are serialized into JSON. From JSON DateTime values are returned as nullable DateTime, which does not have ToString method that accepts date format string. So to use DateTime.ToString(dateformat), you should first cast DateTime? to DateTime:

<<var [dt = ((DateTime)class.date).ToString(“yyyy-MM-dd”) ] >>

and

<<var [dt2 = ((DateTime)nested.nested_date).ToString(“yyyy-MM-dd”) ] >>

Please see the following article for more information:
https://docs.aspose.com/words/net/linq-reporting-engine-api/#accessing-json-data

@alexey.noskov:

It’s working using cast and adding Datetime as knownType of engine

Many Thanks

1 Like