I want to validate a column in Excel using EPPlus for time range like 1:00 PM to 11:00 AM.
Time must be "hh:mm AM/PM" format.
I solved the problem. Here is the code :
var validationEndTime = workSheet.DataValidations.AddTimeValidation("H:H");
validationEndTime.ShowInputMessage = true;
validationEndTime.Prompt = "Add end time in hh:mm AM/PM format as example 9:00 AM or 12:30 PM";
validationEndTime.ErrorStyle = ExcelDataValidationWarningStyle.stop;
validationEndTime.ShowErrorMessage = true;
validationEndTime.Error = "Insert valid time";
validationStartDate.Operator = ExcelDataValidationOperator.between;
var timeEnd = validationStartDate.Formula.Value;
timeEnd.Hour = 00;
timeEnd.Minute = 00;
var timeEnd2 = validationStartDate.Formula2.Value;
timeEnd2.Hour = 23;
timeEnd2.Minute = 59;
validationEndTime.Formula.Value = timeEnd;
validationEndTime.Formula2.Value = timeEnd2;
And this will make a validation in excel like this :
I tried a lot to make DataValidations.AddDateTimeValidation work but it didn't work for me. So instead of looking for solution in C#, I tried to figure out how excel deals with time.
Excel stores time in decimal numbers. So instead of using DataValidations.AddDateTimeValidation, I used
DataValidations.AddDecimalValidation,
I converted the start time and end time in excel to decimal number with 10 digit precision.
So 12:00:00 just becomes 0.00 and 11:59:59 pm becomes 0.999305555555556.
My C# code looks something like this
var validdateTime = sheet1.DataValidations.AddDecimalValidation("H:H");
validdateTime.ShowInputMessage = true;
validdateTime.Prompt = "Add end time in hh:mm AM/PM format as example 9:00 AM or 12:30 PM";
validdateTime.AllowBlank = false;
validdateTime.ShowErrorMessage = true;
validdateTime.Error = "The time entered is not valid";
validdateTime.ErrorStyle = ExcelDataValidationWarningStyle.stop;
validdateTime.Operator = ExcelDataValidationOperator.between;
validdateTime.Formula.Value = 0.0;
validdateTime.Formula2.Value = 0.999305555555556;
Related
I have a JS function that generates today date:
function GetDate(date) {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = dd + '/' + mm + '/' + yyyy;
alert(today);
return today; // 13/03/2021
}
This function returns 13/03/2021
I pass it on to Server Side Code and do this :
DateTime dateToday = DateTime.ParseExact(cdate, "dd/MM/yyyy", CultureInfo.GetCultureInfo("en-AU"));
emailCopy = emailCopy.Replace("{date}", dateToday.ToString("dd MMMM yyyy"));
However here it puts the date as 12 March 2021
Why is it doing that? The date going in is clearly 13/03/2021. Also in next line I pass this date to be added to SQL Server Table:
dateToday.ToString("yyyy-MM-dd")
And the date added to the database is also correct : 2021-03-13.
When you create a new DateTime object, but only set the date part of it, this sets the time to 00:00:00 (midnight). This is in GMT. So when you format the date it takes the date you set at midnight, and converts it to your time zone, which is actually the day before.
You can fix this by doing this "kludge":
var now = DateTime.Now;
var adjusted = new DateTime(
dateToday.Year, dateToday.Month, dateToday.Day, now.Hour, now.Minute, now.Second);
var final = adjusted.ToString("dd MMMM yyyy");
There may be a better way to do this, though.
ETA
You should consider using JavaScript's Date.toISOString() instead of just sending the date. Then in C#, use Convert.ToDateTime() to parse it. That uses UTC and you are guaranteed to get the exact time that the client machine generated the date.
I encountered issue comparing DateTime values from CRM and Excel.
1. DateTime excelDT1 = Convert.ToDateTime(row[Excel.notificationdate].ToString().Trim(), System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat); ;
2. var excelDT2 = row[Excel.notificationdate].ToString().Trim();
3. var excelDT3 = row[Excel.notificationdate];
4. DateTime crmDT1 = Convert.ToDateTime(caseEntity.Attributes[Case.notificationdate].ToString().Trim(), System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat); ;
5. var crmDT2 = caseEntity.Attributes[Case.notificationdate].ToString().Trim();
6. var crmDT3 = caseEntity.Attributes[Case.notificationdate];
Below are the outputs respectively:
excelDT1: 1/13/2020 12:00:00 AM
excelDT2: 13/01/2020
excelDT3: 13/01/2020
crmDT1: 12/1/2020 4:00:00 PM
crmDT2: 1/12/2020 4:00:00 PM
crmDT3: 1/12/2020 4:00:00 PM
May I know how can I format the above such that they can be checked if they are equal?
Thank you.
You can compare 1st and 4th with public static bool Equals (DateTime t1, DateTime t2);
var result = DateTime.Equals(excelDT1, crmDT1);
I have 14 textboxes that takes a user’s input of two times in a 24 hour clock format. When the calculate button is clicked the difference between the two times is calculated and returns the time in decimal format to the respective label. Ideally I would like the user to simply enter time as an integer, such as 1253 or 925 and select AM or PM from the drop down box. Say a user enters 1115 as the in time with AM selected then enters 300 as the out time with PM selected (as shown in the example entry below), the calculate button is clicked and 3.75 is returned in the label.
I have this code below and it works but I get errors when there aren’t exactly four characters. First question, how do I fix this so if an integer such as 800 will be read as 8:00 and not error out?
DateTime dt = DateTime.ParseExact(MondayW1InTextBox.Text, "HHmm", CultureInfo.InvariantCulture);
string timestring = dt.ToString("h:mm");
MondayW1Label.Text = timestring;
Second, once the string is formatted to 12 hour format, how can I get it to take the AM/PM drop down list as an argument for calculating the difference?
Below is the current C# code behind for just the Monday textboxes calculation which is just 24 hour time format, but want to move away from 24 hour time.
protected void CalculateButton_Click(object sender, EventArgs e)
{
TimeSpan TimeIn, TimeOut;
if (!TimeSpan.TryParse(MondayW1InTextBox.Text, out TimeIn)) TimeIn = default(TimeSpan);
if (!TimeSpan.TryParse(MondayW1OutTextBox.Text, out TimeOut)) TimeOut = default(TimeSpan);
MondayW1Label.Text = (TimeOut - TimeIn).TotalHours.ToString("f2");
}
Your first problem is related to the pattern you are using to parse the time: ParseExact will always try to match the exact pattern (in your case, "HHmm") to the string being parsed. That means it expects two digits representing the hours and two digits for the minutes. You can easily make it work if you append a leading zero to your string whenever its size is < 4. You can use the PadLeft method for doing that:
DateTime dt = DateTime.ParseExact(MondayW1InTextBox.Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
The first argument of PadLeft is the total length of the resulting string (in our case, 4), and the second argument is the character that should be used to fill in ('0').
For your second problem, you can parse the strings to get the DateTime object and, if the PM value is selected, just add 12 hours to the corresponding time.
DateTime timeIn = DateTime.ParseExact(MondayW1InTextBox.Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
DateTime timeOut = DateTime.ParseExact(MondayW1OutTextBox.Text.PadLeft(4, '0'), "HHmm", CultureInfo.InvariantCulture);
if(dropDownListIn.SelectedValue == "PM") timeIn = timeIn.AddHours(12);
if(dropDownListOut.SelectedValue == "PM") timeOut = timeOut.AddHours(12);
MondayW1Label.Text = (timeOut - timeIn).TotalHours.ToString("f2");
Notice that you can use the subtraction operator on DateTime objects to get the time difference between them, no need to explicitly convert them to TimeSpans.
Maybe this example (it is pretty crude but you get the logic) will be helpful:
string datetime1 = "800";
DateTime dt1 = DateTime.ParseExact((datetime1.Length == 3) ? "0" + datetime1 : datetime1, "hhmm", CultureInfo.InvariantCulture);
string dropDownVal = "AM";
if (dropDownVal == "PM")
dt1 = dt1.AddHours (12);
string datetime2 = "1100";
DateTime dt2 = DateTime.ParseExact((datetime2.Length == 3) ? "0" + datetime2 : datetime2, "hhmm", CultureInfo.InvariantCulture);
dropDownVal = "PM";
if (dropDownVal == "PM")
dt2 = dt2.AddHours (12);
TimeSpan TimeIn, TimeOut;
TimeIn = new TimeSpan (dt1.Ticks);
TimeOut = new TimeSpan(dt2.Ticks);
Console.WriteLine((TimeOut - TimeIn).TotalHours.ToString("f2"));
Console.ReadLine ( );
I am trying to convert this e.g. 12/31/2012 format into DateTime, however when I run this code the conversion works but the time is not current. I am looking to convert to DateTime but with the current time:
Example: When I run the below code and enter date: 12/31/2012
I get: 12/31/2012 12:00:00 AM
I am not sure how to get the current time instead of 12:00:00 AM
Console.Write("Enter Current Date: ");
string strMyDate = Console.ReadLine();
DateTime dt = DateTime.Parse(strMyDate);
Console.WriteLine(dt);
Console.ReadKey();
You can extract only the time from DateTime.Now by using the TimeOfDay property and add it to your manually entered date, e.g.
var time = dt.Add(DateTime.Now.TimeOfDay);
As an additional note, I would use DateTime.TryParse instead, as the value entered by the user may not be a parseable date, e.g.
DateTime dt;
var isDate = DateTime.TryParse(strMyDate, out dt);
if(isDate)
{
var time = dt.Add(DateTime.Now.TimeOfDay);
}
DateTime.Now will give you current time. Than combine date portion from first value with time portion from Now to get the result.
Add this to your string:
string strMyDate = Console.ReadLine();
strMyDate = string.Format("{0} {1}",
strMyDate,
DateTime.Now.ToShortTimeString());
Haven't actually compiled this but something like this should work:
DateTime dt = DateTime.Parse("12/31/2012 " + DatTime.Now.TimeOfDay.ToString());
You can build up the time from the hours, minutes and seconds portion of DateTime.Now;
string strMyDate = "12/31/2012";
DateTime dt = DateTime.Parse(strMyDate);
DateTime current = DateTime.Now;
dt = dt.AddHours(current.Hour).AddMinutes(current.Minute).AddSeconds(current.Second);
Console.WriteLine(dt);
Now i am working with Hijri dates and trying to convert them to Gregorian dates using the following code :
string HijriDate;
string[] allFormats ={"yyyy/MM/dd","yyyy/M/d",
"dd/MM/yyyy","d/M/yyyy",
"dd/M/yyyy","d/MM/yyyy","yyyy-MM-dd",
"yyyy-M-d","dd-MM-yyyy","d-M-yyyy",
"dd-M-yyyy","d-MM-yyyy","yyyy MM dd",
"yyyy M d","dd MM yyyy","d M yyyy",
"dd M yyyy","d MM yyyy","MM/dd/yyyy"};
CultureInfo enCul = new CultureInfo("en-US");
CultureInfo arCul = new CultureInfo("ar-SA");
arCul.DateTimeFormat.Calendar = new System.Globalization.HijriCalendar();
DateTime tempDate = DateTime.ParseExact(HijriDate, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString("MM/dd/yyyy");
this code is working fine with all dates except the date that has 30th day in month like the following :
30/10/1433, 30/12/1432 or 30/05/1433 etc. so how to handle and convert that date with its corresponding Gregorian
here is the code it is working well
now on this code I'm returning the date from the function as string not as datetime, but you can simply using return datetime type instead on string
public string ConvertDateCalendar(DateTime DateConv, string Calendar, string DateLangCulture)
{
System.Globalization.DateTimeFormatInfo DTFormat;
DateLangCulture = DateLangCulture.ToLower();
/// We can't have the hijri date writen in English. We will get a runtime error - LAITH - 11/13/2005 1:01:45 PM -
if (Calendar == "Hijri" && DateLangCulture.StartsWith("en-"))
{
DateLangCulture = "ar-sa";
}
/// Set the date time format to the given culture - LAITH - 11/13/2005 1:04:22 PM -
DTFormat = new System.Globalization.CultureInfo(DateLangCulture, false).DateTimeFormat;
/// Set the calendar property of the date time format to the given calendar - LAITH - 11/13/2005 1:04:52 PM -
switch (Calendar)
{
case "Hijri":
DTFormat.Calendar = new System.Globalization.HijriCalendar();
break;
case "Gregorian":
DTFormat.Calendar = new System.Globalization.GregorianCalendar();
break;
default:
return "";
}
/// We format the date structure to whatever we want - LAITH - 11/13/2005 1:05:39 PM -
DTFormat.ShortDatePattern = "dd/MM/yyyy";
return (DateConv.Date.ToString("f", DTFormat));
}
To call this Method here are an example
ltrCalValue.Text = ConvertDateCalendar(CalHijri.SelectedDate, "Gregorian", "en-US");
To call the Hijri
ltrCalValue.Text = ConvertDateCalendar(CalHijri.SelectedDate, "Hijri", "en-US");
Max Value of a days in a month can be calculated by DateTime.DaysInMonth(year, month)
and use it this way
int result = DateTime.DaysInMonth(2012, 2); // returns 29 being a leap year
but
int result = DateTime.DaysInMonth(2011, 2) // returns 28 being a non-leap year
10th and 12nd months in Hirji don't have 30th day, so that date is invalid.