Working with Time Function Formulas
13 Jul 20264 minutes to read
Time function formulas return the current date, the current date and time, or a date-time value derived from cell arguments. Syncfusion Flutter XlsIO supports the following time functions:
- NOW — returns the serial number of the current date and time.
- TODAY — returns the serial number of the current date.
NOTE
Both
NOWandTODAYare recalculated every time the workbook is loaded into Syncfusion Flutter XlsIO. The returned value reflects the date and time of the device that performs the calculation.
For prerequisites and installation steps, see the Flutter XlsIO Overview. For background on formulas and how to enable calculation, see Working with Formulas.
NOTE
The code samples in this document use
await workbook.save(). Always callworkbook.dispose()after saving to release the XlsIO DOM memory, ideally inside atry/finallyblock. Each function sample callsenableSheetCalculations()so the calculated value is available through thecalculatedValueproperty of aRange.
NOW function
The NOW function returns the serial number of the current date and time. It takes no arguments. The returned value is a numeric serial number; apply a number format such as m/d/yyyy h:mm to display it as a date and time.
import 'package:syncfusion_flutter_xlsio/xlsio.dart';
Future<void> nowFormula() async {
final Workbook workbook = Workbook();
final Worksheet sheet = workbook.worksheets[0];
sheet.enableSheetCalculations();
// Write the current date and time to A1.
final Range range = sheet.getRangeByName('A1');
range.setFormula('=NOW()');
// Apply a date-time number format so the value is displayed as a date and time.
range.numberFormat = 'm/d/yyyy h:mm';
// The calculated value is available as a string. Parse it to a double if needed.
final String result = range.calculatedValue;
// ignore: avoid_print
print(result);
final List<int> bytes = await workbook.save();
workbook.dispose();
}TODAY function
The TODAY function returns the serial number of the current date. It takes no arguments. The returned value is a numeric serial number; apply a number format such as mm/dd/yyyy to display it as a date.
import 'package:syncfusion_flutter_xlsio/xlsio.dart';
Future<void> todayFormula() async {
final Workbook workbook = Workbook();
final Worksheet sheet = workbook.worksheets[0];
sheet.enableSheetCalculations();
// Write the current date to A1.
final Range range = sheet.getRangeByName('A1');
range.setFormula('=TODAY()');
// Apply a date number format so the value is displayed as a date.
range.numberFormat = 'mm/dd/yyyy';
final List<int> bytes = await workbook.save();
workbook.dispose();
}Related functions
The following time-related functions are also supported by Syncfusion Flutter XlsIO:
| Function | Description |
|---|---|
DATE |
Returns the serial number of a date assembled from year, month, and day arguments. |
TIME |
Returns the serial number of a time assembled from hour, minute, and second arguments. |
YEAR, MONTH, DAY
|
Extract the corresponding component from a date. |
HOUR, MINUTE, SECOND
|
Extract the corresponding component from a time. |
DATEVALUE, TIMEVALUE
|
Convert a text representation of a date or time to a serial number. |
For the complete list of supported format codes, see Working with Cell Formatting.