I need to create a midnight DateTime
I've just done this:
DateTime endTime = DateTime.Now;
endTime.Subtract(endTime.TimeOfDay);
Haven't test it yet, I'm assuming it works but is there a better/cleaner way?
Just use foo.Date, or DateTime.Today for today's date
DateTime endTime = DateTime.Now.Date;
Now endTime.TimeOfDay.ToString() returns "00:00:00"
DateTime.Now . AddDays(1) . Date
DateTime.Today
You can use DateTime.Today with exact seconds of the midnight.
DateTime today = DateTime.Today;
DateTime mid = today.AddDays(1).AddSeconds(-1);
Console.WriteLine(string.Format("Today: {0} , Mid Night: {1}", today.ToString(), mid.ToString()));
Console.ReadLine();
This should print :
Today: 11/24/2016 10:00:00 AM , Mid Night: 11/24/2016 11:59:59 PM
var dateMidnight = DateTime.ParseExact(DateTime.Now.ToString("yyyyMMdd"), "yyyyMMdd", CultureInfo.InvariantCulture);
private bool IsServiceDatabaseProcessReadyToStart()
{
bool isGoodParms = true;
DateTime currentTime = DateTime.Now;
//24 Hour Clock
string[] timeSpan = currentTime.ToString("HH:mm:ss").Split(':');
//Default to Noon
int hr = 12;
int mn = 0;
int sc = 0;
if (!string.IsNullOrEmpty(timeSpan[0]))
{
hr = Convert.ToInt32(timeSpan[0]);
}
else
{
isGoodParms = false;
}
if (!string.IsNullOrEmpty(timeSpan[1]))
{
mn = Convert.ToInt32(timeSpan[1]);
}
else
{
isGoodParms = false;
}
if (!string.IsNullOrEmpty(timeSpan[2]))
{
sc = Convert.ToInt32(timeSpan[2]);
}
else
{
isGoodParms = false;
}
if (isGoodParms == true )
{
TimeSpan currentTimeSpan = new TimeSpan(hr, mn, sc);
TimeSpan minTimeSpan = new TimeSpan(0, 0, 0);
TimeSpan maxTimeSpan = new TimeSpan(0, 04, 59);
if (currentTimeSpan >= minTimeSpan && currentTimeSpan <= maxTimeSpan)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Related
The problem is that I need to find each Saturday and Friday night and charge them $140 on Friday or Saturday. Should I use a while loop?
DateTime ArrivalDate = DateTime.Parse(txtArrivalDate.Text);
DateTime DepartureDate = DateTime.Parse(txtDepartureDate.Text);
TimeSpan numberOfNights = DepartureDate.Subtract(ArrivalDate);
decimal nights = numberOfNights.Days;
txtNights.Text = nights.ToString();
//txtTotalPrice
decimal pricePerNight = 120.00m;
decimal totalNight = nights * pricePerNight;
txtTotalPrice.Text = "$" + totalNight.ToString();
txtAvgPrice.Text = "$" + pricePerNight.ToString();
// finds the fridays
decimal morePricePerNight = 140.00m;
int i = 0;
// while loop
while (ArrivalDate.DayOfWeek != DayOfWeek.Friday)
{
ArrivalDate = ArrivalDate.AddDays(1);
}
while (DepartureDate.DayOfWeek != DayOfWeek.Friday)
{
DepartureDate = DepartureDate.AddDays(1);
}
Why not to use just a one while loop?
I guess you can write the following code:
var date = DepartureDate;
while (date < ArrivalDate)
{
if (date.DayOfWeek == DayOfWeek.Friday || date.DayOfWeek == DayOfWeek.Saturday)
{
totalNight += 140.00m;
}
date = date.AddDays(1);
}
When I calculated the duration between 28/07/2018 and 01/08/2018, its result was two days. the correct answer is four days. what is my mistake?
my Code is :
private static double DateDurationCalculate(DateTime startTime, DateTime endTime)
{
TimeSpan span = endTime.Subtract(startTime);
return span.TotalDays;
}
startTime is 2018/07/28 11:54 and endTime is 2018/08/01 09:28.
Try like this,
DateTime startTime = DateTime.Parse("2018/07/28 11:54");
DateTime endTime = DateTime.Parse("2018/08/01 09:28");
private static double DateDurationCalculate(DateTime startTime, DateTime endTime)
{
startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0);
endTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, endTime.Hour, endTime.Minute, 0);
TimeSpan span = endTime.Date.Subtract(startTime.Date);
return span.TotalDays;
}
Thanks to everyone, my problem was solved with the following method.
private static double DateDurationCalculate(DateTime startTime, DateTime endTime)
{
var usCulture = "en-US";
startTime = DateTime.Parse(startTime.ToString("MM/dd/yyyy"), new CultureInfo(usCulture, true));
endTime = DateTime.Parse(endTime.ToString("MM/dd/yyyy"), new CultureInfo(usCulture, true));
TimeSpan span = endTime.Date.Subtract(startTime);
return span.TotalDays;
}
How to make newTime as a global variable?
It should be added 30 minutes in time and use it in a condition. It is necessary to make var newTime as global var.
if (timerCheck == 0)
{
var today = DateTime.Now;
var interval = new TimeSpan(00, 30, 00);
var newTime = today + interval;
timerCheck = 1;
}
if (timerCheck == 1)
{
var today = DateTime.Now;
if (today >= newTime)
{
You can do it like this
public static class GlobalVariables
{
public static DateTime NewTime { get; set; }
}
Then call it like this:
if (today >= GlobalVariables.NewTime)
Just move it outside if.
I don't test it, but It might work.
DateTime newTime;
if (timerCheck == 0)
{
var today = DateTime.Now;
var interval = new TimeSpan(00, 30, 00);
newTime = today + interval;
timerCheck = 1;
}
if (timerCheck == 1)
{
if (newTime.Equals(default(DateTime)) return; // don't sure is it required or not
var today = DateTime.Now;
if (today >= newTime)
{
Well i think you should put it outside the body of If first as a variable then use it in the condition.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class calenderdisp : System.Web.UI.Page
{
DateTime dt = DateTime.Now;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
if (ddlweeklist.SelectedItem.Text == "Today")
{
txtstart.Text = DateTime.Now.ToString("dd/MM/yyyy");
Txtend.Text = DateTime.Today.ToString("dd/MM/yyyy");
}
}
}
protected void ddlweeklist_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlweeklist.SelectedItem.Text == "This Week")
{
int st = DayOfWeek.Sunday - dt.DayOfWeek;
int en = DayOfWeek.Saturday - dt.DayOfWeek;
txtstart.Text = dt.AddDays(st).ToString("dd/MM/yyyy");
Txtend.Text = dt.AddDays(en).ToString("dd/MM/yyyy");
}
if (ddlweeklist.SelectedItem.Text == "Next Week")
{
DateTime dt1 = dt.AddDays(7);
int st = DayOfWeek.Sunday - dt1.DayOfWeek;
int en = DayOfWeek.Saturday - dt1.DayOfWeek;
txtstart.Text = dt1.AddDays(st).ToString("dd/MM/yyyy");
Txtend.Text = dt1.AddDays(en).ToString("dd/MM/yyyy");
}
if (ddlweeklist.SelectedItem.Text == "Last Week")
{
DateTime dt2 = dt.AddDays(-7);
int st = DayOfWeek.Sunday - dt2.DayOfWeek;
int en = DayOfWeek.Saturday - dt2.DayOfWeek;
txtstart.Text = dt2.AddDays(st).ToString("dd/MM/yyyy");
Txtend.Text = dt2.AddDays(en).ToString("dd/MM/yyyy");
}
if (ddlweeklist.SelectedItem.Text == "This Month")
{
DateTime stmonth = new DateTime(dt.Year, dt.Month, 1);
DateTime enmnth = new DateTime(dt.Year, dt.Month, DateTime.DaysInMonth(dt.Year, dt.Month));
txtstart.Text = stmonth.ToString("dd/MM/yyyy");
Txtend.Text = enmnth.ToString("dd/MM/yyyy");
txtnodm.Text = DateTime.DaysInMonth(dt.Year, dt.Month).ToString();
Isleap();
}
}
}
now got the answer...thank u .........but how this can be done using java script as i was very much interested to learn it so please kindly give suggestions to this code to be written in javascript
and i will be glad if u let me know the standard book to be followed to learn java script
Please try with the below code snippet.
DateTime dt = DateTime.Now; //Your Date
DateTime start = new DateTime(dt.Year, dt.Month, 1); //First Date of the month
DateTime end = start.AddMonths(1).AddDays(-1); //Last Date of the month
string startDay = start.DayOfWeek.ToString(); //First weekday of the month
string endDay = end.DayOfWeek.ToString(); //Last weekday of the month
var d = DateTime.Today;
// Start
d.AddDays(-d.Day+1);
// End
d.AddMonths(1).AddDays(-d.Day).Dump();
The first day is easy with the DateTime constructor. The last day is one day less than the first day of the next month.
public static void FirstAndLastDayOfMonth(DateTime date, out DateTime first, out DateTime last) {
first = new DateTime(date.Year, date.Month, 1);
DateTime nextFirst;
if (first.Month == 12) nextFirst = new DateTime(first.Year + 1, 1, 1);
else nextFirst = new DateTime(first.Year, first.Month + 1, 1);
last = nextFirst.AddDays(-1);
}
you can also get last day this way:
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
or:
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, 1).AddMonths(1).AddDays(-1);
This link may also help you:
http://www.c-sharpcorner.com/UploadFile/scottlysle/FirstAndLastDay10262007135750PM/FirstAndLastDay.aspx
DateTime dt = DateTime.Now.AddDays(-(DateTime.Now.Day - 1));
int lday= DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
DateTime DTY = new DateTime(DateTime.Now.Year, DateTime.Now.Month, lday);
MessageBox.Show("First Day :- "+dt.DayOfWeek.ToString());
MessageBox.Show("Last Day :-" + DTY.DayOfWeek.ToString());
To get first day of month:
String day = System.DateTime.Now.DayOfWeek.ToString();
int date = System.DateTime.Now.Day;
String fdm = System.DateTime.Now.AddDays(-Convert.ToDouble(date - 1)).DayOfWeek.ToString();
To get last day of month:
var now = DateTime.Now;
var startOfMonth = new DateTime(now.Year, now.Month, 1);
var DaysInMonth = DateTime.DaysInMonth(now.Year, now.Month);
var lastDay = new DateTime(now.Year, now.Month, DaysInMonth);
string lastday = lastDay.DayOfWeek.ToString();
I want to compare two dates. In pseudo-code:
If the dueDate > now or dueDate = now
Then Fine Amount = something.
Else Fine Amount = 0
I wrote below code:
DateTime dueDate = Convert.ToDateTime(Reader1[3].ToString());
DateTime now = DateTime.Now;
int result = DateTime.Compare(dueDate, now);
if ((result < 0) || (result == 1))
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString());
}
else
{
row["Fine_Amount"] = 0;
}
This code gives wrong value, when
dueDate = 23-12-2011 AM 12:00:00
now = 23-12-2011 PM 05:26:54
I want to Compare:
dueDate = 23-12-2011
now = 23-12-2011
How do I remove the time in that?.
Adding below code is given result. But its to lengthy code.: -
DateTime dueDate = Convert.ToDateTime(Reader1[3].ToString());
DateTime now = DateTime.Now;
if (dueDate.Year < now.Year)
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString());
}
else if (dueDate.Year > now.Year)
{
row["Fine_Amount"] = 0;
}
else if (dueDate.Year == now.Year)
{
if (dueDate.Month < now.Month)
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString();
}
else if(dueDate.Month > now.Month)
{
row["Fine_Amount"] = 0;
}
else if(dueDate.Month == now.Month)
{
if(dueDate.Day < now.Day)
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString();
}
else
{
row["Fine_Amount"] = 0;
}
}
}
Is there any way to short this code?.
Answer For this Question is
if (dueDate.Date >= now.Date)
{
row["Fine_Amount"] = 0;
}
else
{
row["Fine_Amount"] = Convert.ToDouble(Reader1[4].ToString());
}
This datetime.date is gives
dueDate = 23-12-2011 AM 12:00:00 to 23-12-2011 AM 12:00:00
now = 23-12-2011 PM 05:26:54 to 23-12-2011 AM 12:00:00
You can use DateTime.Now.Date
According to docs, Date property returns: A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
The DateTime class in C# supports comparison by simply using <, >, and == operators. Do it like your above written pseudo code.
dueDate.Date >= DateTime.Now.Date
Although you could use the .Date property of DateTime it is generally a good practice to compare a certain date to a date range:
startDate <= someDate && someDate < endDate
You can use the DateTime.Date Property to gets the date component of a DateTime value:
if (dueDate.Date >= DateTime.Now.Date)
use the Date property of DateTime object for comparison