I am parsing an excel document in excel using the excel: ^1.1.5 package. In my sheet, i have a Date column and this Date is being received in my Flutter code in the following format
"44663"
rather than:
"2022/04/12"
How do I parse this to a format such as YY-MM-DD.
I have tried DateTime.parse(), but it throws an error that my date format is invalid.
I found the answer :
const gsDateBase = 2209161600 / 86400;
const gsDateFactor = 86400000;
final date = double.tryParse("44663");
if (date == null) return null;
final millis = (date - gsDateBase) * gsDateFactor;
print(DateTime.fromMillisecondsSinceEpoch(millis.toInt(), isUtc: true));
Related
I need to format the date in Excel while writing in the Excel sheet.
It should display this format "MM/dd" but when click on this then formula bar show the complete date "MM/dd/yyyy".
For reference I have attached the screenshot as well. Please check and suggest how I can achieve this in EPPlus Code.
I have tried the following code but no luck:
namedWorksheet.Cells["E4"].Style.Numberformat.Format = "M/dd/yyyy";
namedWorksheet.Cells["E4"].Value = DateTime.Now.ToString("MM/dd/yyyy").Substring(0,4);
Here is the solution for this issue.
Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL")
{
DateTimeFormat = { YearMonthPattern = "MM/dd" }
};
namedWorksheet.Cells["G4"].Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.YearMonthPattern;
var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
namedWorksheet.Cells["G4"].Value = date;
I've a problem when I'm reading a Date value from columns in a excel file using exceljs package, I can't get the truth value in the correct format like a Date data type, instead I get a number.
My project is build with Nest, and I use exceljs to write/read xlsx files, the code is:
worksheet.eachRow({ includeEmpty: true }, (row, rowNumber) => {
row.eachCell((cell, colNumber) => {
...
const header = headers[colNumber - 1];
switch (header) {
...
case 'dateColumn':
// Date format is dd/mm/yyyy
// Original value in excel is '16/06/2020'
const cellValue: Date = cell.value as Date; // When i get value from cell is 43998
console.log(cellValue); // print 43998
break;
...
}
}
The cell format is 'dd/mm/yyyy'
You will need to convert Excel Date Serial Number to JS Date.
Here is a very basic example:
const date0 = new Date(0);
const utcOffset = date0.getTimezoneOffset();
const cellValue = new Date(0, 0, cell.value - 1, 0, -utcOffset, 0);
To convert Excel Serial Date to normal you can use
new Date(Date.UTC(0, 0, cell.value - 1, 0, 0, 0))
Selenium is incorrectly reading the date as 43095 when I enter 26-12-2017. How to get Selenium to read the correct date?
for (int i=0;i<=TcRow;i++)
{
for (int j=0;j<TcCol;j++)
{
Cell Cell=TcSheet.getRow(i).getCell(j);
}
}
Am I reading the format incorrectly?
TcSheet.getRow(i).getCell(j).setCellType(Cell.CELL_TYPE_STRING);
What changes do I need to do here to make sure they read both the string and the date field?
data[i][j]=TcSheet.getRow(i).getCell(j).getStringCellValue();
}
I also faced the same issue during reading the excel file where I'm fetching date is formatted in dd/mm/yyyy format and selenium fetching wrong value.
For that, I have used DataFormatter. It will returns Excel cell value with format e.g. Date format 15-04-208 in excellent then it will returns date with same format. Look below code that I used in my Framework. Hope it will also work for you.
FileInputStream fis = new FileInputStream("path\\to\\file.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(worksheet);
DataFormatter formatter = new DataFormatter();
Cell cell = sheet.getRow(rowNum).getCell(cellNum);
String cellValue = formatter.formatCellValue(cell);
System.out.println(cellValue);
return cellValue;
Let me know if you have any query.
After watching David Leedy's video on creating pdfs from xpages using iText - I tried to create a pdf of a form in one of our XPage apps. The form is basically a 2 column table with various data fields - name, address etc. All of the text fields display perfectly but not the date fields. Here is one example where I am trying to display the date of birth of the person (Person_dob) in cell 7 of my table.
var cell7 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(currdoc.getItemValueString("Person_dob")));
This displays a blank in the table cell, not the value of the date field; I have tried changing the code and used both getItemValueDate and getItemValue instead of getItemValueString but this crashes the pdf before it even creates. No doubt I am missing something obvious but I just cannot see it.
Use getItemValueDate() to get date as Date object and convert it to a string in locale date format with toLocaleDateString():
var date = currdoc.getItemValueDate("Person_dob");
if (date != null) {
var cell7 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(date.toLocaleDateString()));
}
If you wish more control over the date format then you can use SimpleDateFormat in instead:
var date = currdoc.getItemValueDate("Person_dob");
if (date != null) {
var datePattern = new java.text.SimpleDateFormat("yyyy-MM-dd");
var cell7 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(datePattern.format(date)));
}
The datetime I'm writing to Excel always get rounded up to the next day:
workSheet.Cells[0, 0] = new Cell(DateTime.Now, new CellFormat(CellFormatType.DateTime, #"HH:mm:ss"));
In the output Excel file the cell gets this value: 29/09/2013 00:00:00
The DateTime.Now from this example is 28/09/2013 19:42:23
I ended up passing the cell value as a string instead of as a DateTime:
workSheet.Cells[0, 0] = new Cell(DateTime.Now.ToString(#"HH:mm:ss:ff"),
new CellFormat(CellFormatType.DateTime, #"HH:mm:ss"));
If you are using the ExcelLibrary Project Source Code, you can fix this by:
Go to SharedResource Class in this location: [Project Source Code folder]\Office\BinaryFileFormat folder
Change the EncodeDateTime function as below:
public double EncodeDateTime(DateTime value)
{
double days = (value - BaseDate).Days;
//if (days > 365) days++;
return days;
}
Pass the DataTime object to the Cell with the prefered format:
worksheet.Cells[iIndex, j] = new Cell(((DateTime)cellValue), new CellFormat(CellFormatType.DateTime, #"dd/MM/yyyy"));
You need to convert the date format from OLE Automation to the .net format by using DateTime.FromOADate.
If oCell.Format.FormatType = CellFormatType.Date OrElse oCell.Format.FormatType = CellFormatType.DateTime Then
Dim d As Double = Double.Parse(oCell.Value)
Debug.print(DateTime.FromOADate(d))
End If