Check Date is tomorrow in C#.net - asp.net

I write some code to check that now date is tomorrow :
re_dat = SDKClass.Selct_Date_now(); // return today date from database.
DateTime date_now = DateTime.Parse(re_dat).Date;
if (date_now == DateTime.Now.AddDays(1).Date)
{
response.write("tomorrow");
}
but condition doesn't fire.
thanks a lot.

If your question is about difference with some dynamic input date and today date as reference date, then you can use dates difference like this:
var dateDiff = (DateTime.Today - inputDate.Date).TotalDays;
Here is my simple version of date formatting:
public static string GetDateString(DateTime inputDate)
{
var dateDiff = (DateTime.Today - inputDate.Date).TotalDays;
if (dateDiff == 0)
{
return "TODAY";
}
else if(dateDiff == 1)
{
return "YESTERDAY";
}
else if(dateDiff == -1)
{
return "TOMORROW";
}
else
{
return inputDate.ToShortDateString();
}
}

you shouldn't retrive the today date from database.
i think best solutin for this:
if (DateTime.Today == DateTime.Today.AddDays(1))
{
Response.Write("Tomorrow");
}
else { //Write whatever you want }

Related

How to fix Google Apps Script for local time and automatically clear a cell

I am creating a shared agenda that has to be filled by different people. Everytime they modify a certain cell, the date of change (day) is reported in another cell on the same row. The code below is currently working, but there are 3 minor problems:
The local time is not correct (I am living in italy)
I would like to see the time as well (hours:minutes)
If the modified cell is cleared (like if the text is deleted), also the date of change should disappear.
I find this type of code difficult to debug and would appreciate any help.
Thank you in advance,
function onEdit(event) {
var cell = event.source.getActiveCell();
var sheet = cell.getSheet();
var headers= sheet.getDataRange().getValues()[0];
var statusCol = headers.indexOf('Status') + 1 ;
var ss = event.source.getActiveSheet();
if (event.range.columnStart != 2) {
} else {
ss.getRange(event.range.rowStart, 5)
.setValue(getDate(new Date()));
}
var tt = event.source.getActiveSheet();
if (event.range.columnStart != 34) {
return;
} else {
tt.getRange(event.range.rowStart, 37)
.setValue(getDate(new Date()));
}
}
function getDate(d) {
if (d) {
return d.getDate() + "/" + (d.getMonth()+1) + "/" + d.getYear();
}
}
Add TimeZone, Hours, Minutes and deletes Datetime if cell contents is deleted.
It adds hours and minutes in 24 hour format with a timezone correction and if e.value is null then it deletes the contents of the datetime cell.
function onEdit(e) {
var ss=e.source;
var rg=e.range;
var sh=rg.getSheet();
if(rg.columnStart!=2 && rg.columnStart!=34){return;}
if(rg.columnStart==2){
if(e.value) {
sh.getRange(rg.rowStart, 5).setValue(Utilities.formatDate(new Date(new Date().getFullYear(),new Date().getMonth()+1,new Date().getDate(),new Date().getHours(),new Date().getMinutes()),Session.getScriptTimeZone(),"d/MMM/yyyy HH:mm"));
}else{
sh.getRange(rg.rowStart, 5).setValue('');
}
}
if (rg.columnStart==34) {
if(e.value) {
sh.getRange(rg.rowStart, 37).setValue(Utilities.formatDate(new Date(new Date().getFullYear(),new Date().getMonth()+1,new Date().getDate(),new Date().getHours(),new Date().getMinutes()),Session.getScriptTimeZone(),"d/MMM/yyyy HH:mm"));
}else{
sh.getRange(rg.rowStart, 37).setValue('');
}
}
}
Spreadsheet Edit Event Object
Utilities.formatDate()

Removing deprecation warning from momentjs

I'm using momentjs to work with dates in my project for when a user enters a date in M/D/YYYY format to revert to MM/DD/YYYY format (e.g. 2/5/2017 to 02/05/2017). I am also converting any invalid dates or dates greater than today to be reset back to today's date.
element.on("blur", function() {
var currentDate = moment().format('MM/DD/YYYY');
var formattedInput;
if (ctrl.$modelValue !== undefined && ctrl.$modelValue !== "") {
if(moment(ctrl.$modelValue, "MM/DD/YYYY", true).isValid()) {
formattedInput = moment(ctrl.$modelValue);
formattedInput.format('MM/DD/YYYY');
if (formattedInput.isAfter(currentDate)) {
ctrl.$setViewValue(currentDate);
ctrl.$render();
}
} else if (moment(ctrl.$modelValue, "M/D/YYYY", true).isValid()) {
formattedInput = moment(ctrl.$modelValue);
formattedInput.format('MM/DD/YYYY');
if (formattedInput.isAfter(currentDate)) {
ctrl.$setViewValue(currentDate);
ctrl.$render();
} else {
ctrl.$setViewValue(formattedInput.format('MM/DD/YYYY'));
ctrl.$render();
}
} else {
ctrl.$setViewValue(currentDate);
ctrl.$render();
}
}
});
As far as I can tell, this is all working fine with the code I have above. But regardless of working functionality, I am receiving the deprecation warning for non-ISO dates. My thoughts are the use of MM/DD/YYYY format, however this is unchangeable due to business requirements. Is there a way to remedy this issue in a non-cumbersome way?
The problem is with formattedInput = moment(ctrl.$modelValue) here you are using moment parsing without format with non-ISO dates. To remove the Deprecation warning, just use moment(ctrl.$modelValue, "MM/DD/YYYY") and moment(ctrl.$modelValue, "M/D/YYYY") as you have done in the if condition.
Your complete code will be the following:
element.on("blur", function() {
var currentDate = moment();
var formattedInput;
if (ctrl.$modelValue !== undefined && ctrl.$modelValue !== "") {
if(moment(ctrl.$modelValue, "MM/DD/YYYY", true).isValid()) {
formattedInput = moment(ctrl.$modelValue, "MM/DD/YYYY", true);
// This line returns a string, but does not assign to value, so it's superfluous
//formattedInput.format('MM/DD/YYYY');
if (formattedInput.isAfter(currentDate)) {
ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
ctrl.$render();
}
} else if (moment(ctrl.$modelValue, "M/D/YYYY", true).isValid()) {
formattedInput = moment(ctrl.$modelValue, "M/D/YYYY", true);
// see previous comment
//formattedInput.format('MM/DD/YYYY');
if (formattedInput.isAfter(currentDate)) {
ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
ctrl.$render();
} else {
ctrl.$setViewValue(formattedInput.format('MM/DD/YYYY'));
ctrl.$render();
}
} else {
ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
ctrl.$render();
}
}
});
Be sure to fully understand the difference between moment parsing (build a moment object from a string) and moment format (display a string representation of a moment object).

javafx: how to check if a date is greater than today's date?

If I have:
DatePicker dp = new DataPicker();
and at some point I want to know if the data is greater than today, how can I do it?
Example:
if I want to book a room in a hotel from 21/04/2014 well, it should be impossible because today is 28/07/2014.
How can I do it in JavaFX ?
To ensure that a given Date chosenDate is after today, you can check
if (chosenDate.after(new Date())) {
// valid (Date > today)
} else {
// invalid (Date <= today)
}
Note that chosenDate should be a Date with hour, minute and second set to 0 since else it could accept a Date with the same day as today but a later hour than now.
You can write a custom method, which will compare given dates of given date format, and return true, when current date is "older" than your date of interest, eg:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
public static void main(String args[]) {
System.out.println(isDateOfInterestValid("yyyy-mm-dd",
"2014-08-25", "2014-08-28"));
}
public static boolean isDateOfInterestValid(String dateformat,
String currentDate, String dateOfInterest) {
String format = dateformat;
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date cd = null; // current date
Date doi = null; // date of interest
try {
cd = sdf.parse(currentDate);
doi = sdf.parse(dateOfInterest);
} catch (ParseException e) {
e.printStackTrace();
}
long diff = cd.getTime() - doi.getTime();
int diffDays = (int) (diff / (24 * 1000 * 60 * 60));
if (diffDays > 0) {
return false;
} else {
return true;
}
}
}
And in context of pure JavaFX you can get the String value of DatePicker chosen date by calling DatePicker.getValue().toString().
PS In case You have only one DatePicker object, You can use "hidden" method, which will check the current date. It can look like this:
public static String currentDate(String separator) {
Calendar date = new GregorianCalendar();
String day = Integer.toString(date.get(Calendar.DAY_OF_MONTH));
String month = Integer.toString(date.get(Calendar.MONTH) + 1);
String year = Integer.toString(date.get(Calendar.YEAR));
if (month.length() < 2) {
month = "0" + month;
}
if (day.length() < 2) {
day = "0" + day;
}
String regDate = year + separator + month + separator + day;
return regDate;
}

Getting all my posts for a specific period (lambda expression)

I would like a lambda expression to get all my posts with a PublishDate in a specific month / year range (like 10/2011).
public IEnumerable<Post> SearchPosts(string periode)
{
// periode may be 10/2011 so I would like all posts from 10/01/2011 to 10/31/2011
return m_PostRepository.GetPosts().Where(x => x.PublishDate...?
}
Description
You can do this using the DateTime properties Year and Month in your Where Filter.
Sample
return m_PostRepository.GetPosts().Where(x => x.PublishDate.Year == 2011 &&
x.PublishDate.Month == 10).ToList();
More Information
MSDN - DateTime.Month Property
MSDN - DateTime.Year Property
Update after a comment from Bronzato
DateTime? date;
// does date has a value ? If yes, apply the filter. If not return everything.
if (date.HasValue)
{
return m_PostRepository.GetPosts().Where(x => x.PublishDate.Year == date.Value.Year &&
x.PublishDate.Month == date.Value.Month).ToList();
} else return return m_PostRepository.GetPosts();
You can also try it like this (working with PublishDate as Nullable<DateTime>):
DateTime date;
if (DateTime.TryParseExact(value, "MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
var result = m_PostRepository.GetPosts().Where(x => x.PublishDate.HasValue &&
x.PublishDate.Value.Month == date.Month &&
x.PublishDate.Value.Year == date.Year).ToList();
}
public IEnumerable<Post> SearchPosts(string periode){
string[] _periode = periode.Split("/");
return m_PostRepository.GetPosts().Where(x => x.PublishDate.year = convert.ToInt16(_periode[1])).Where(x => x.PublishDate.Month= convert.ToInt16(_periode[0]))}

leave if statement

I have an if statement inside an if statement.
If the condition in the second if statement returns false, I want to go to the first else
because there it sets my validation controls automatically.
I hope you understand
if (page.isvalid() )
{
if (datetime.tryparse (date) == true)
{
// ok
}
else
{
//go to the other else
}
}
else
{
// want to go here
}
Edit:
Important is that I have to first validate the page because after the validation, I know I can parse the datetimes from the 2 input controls and check if the second one is greater than the first one. Otherwise it throws an exception, maybe, if the date is not valid.
instead of DateTime.Parse(date) use
DateTime dt;
bool isParsed = DateTime.TryParse(date, out dt);
//if ( page.isvalid() && (datetime.parse (date) == true) )
if ( page.isvalid() && isParsed )
{
// ok
}
else
{
// want to go here
}
Take out the elses, and it should be what you're looking for. Also, add a return statement after everything is good to go.
if ( page.isvalid() )
{
if (datetime.parse (date) == true)
{
// ok
}
return;
}
// code here happens when it's not valid.
This does exactly what you want, I believe.
if (page.isvalid() && datetime.tryparse(date) == true)
{
// ok
}
else
{
// want to go here
}
It is not clear whether the '== true' is necessary; you might be able to drop that condition.
Exactly what you want to do is impossible. There are two options. One is to determine both answers at the top of your if clause. This is what the other posters are telling you to do. Another option would be something like this:
bool isvalid = true;
if ( page.isvalid() )
{
if (datetime.tryparse (date) == true)
{
// ok
}
else
{
isvalid = false;
}
}
else
{
isvalid = false;
}
if (isvalid == false)
{
//do whatever error handling you want
}
This extracts the error handling code from your else clause and puts it somewhere both code paths can reach.

Resources