Hi,
wie ziehe ich von dem Aktuellen datum, einen tag ab??
lg
kInGoFcHaOs
P,S ich komm bei DateTime.Now.Subtract iwie zu keinem Ergebniss
Hallo kInGoFcHaOs,
DateTime ist immutable, ändert sich also nicht. Alle Operationen liefern das Ergebnis als Rückgabewert.
herbivore
Hallo kInGoFcHaOs,
DateTime.AddDays
ansonsten ist deine Frage ist bereits beantwortet.
Bitte beachte [Hinweis] Wie poste ich richtig? Punkt 1.1.1.
herbivore
DateTime dt = new DateTime(2008, 11, 19); dt = dt.AddDays(-1);
siehe Hilfe 🙂
hmmm okay so gehts schonmal ich habs jetzt mal so gemacht:
int day = DateTime.Today.Day;
int month = DateTime.Today.Month;
int Year = DateTime.Today.Year;
DateTime dt = new DateTime(Year, month, day);
dt = dt.AddDays(-1);
ja ich weis das mit den int day usw könnt ich auch im DateTime dt = new DateTime reinmachen 😉
Vielen dank für eure hilfe 😉
ich habs jetzt mal so gemacht:
int day = DateTime.Today.Day; int month = DateTime.Today.Month; int Year = DateTime.Today.Year; DateTime dt = new DateTime(Year, month, day); dt = dt.AddDays(-1);
ja ich weis das mit den int day usw könnt ich auch im DateTime dt = new DateTime reinmachen 😉
Und wieso nicht
DateTime dt = DateTime.Now.AddDays(-1);
// oder
DateTime dt = DateTime.Now.AddDays(-1).Date;
**:::
Man kann das gestrige Datum auch noch "schöner" bestimmen:
DateTime dt;
if (DateTime.Today.Day == 1) {
switch(DateTime.Today.Month) {
case 1:
dt = new DateTime(DateTime.Today.Year-1, 12, 31);
break;
case 2:
case 4:
case 6:
case 9:
case 11:
dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month-1, 31);
break;
case 3:
dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month-1,
DateTime.IsLeapYear(DateTime.Today.Year) ? 29 : 28);
break;
default:
dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month-1, 30);
break;
}
} else {
dt = new DateTime(
DateTime.Today.Year,
DateTime.Today.Month,
DateTime.Today.Day - 1);
}
Ohne Gewähr. Leider ist mir auf die Schnelle keine komplexe Rechenvorschrift mit dem Ergebnis -1 eingefallen, sonst hätte ich es noch ausgeweitet.
Bei dieser Antwort ließ ich mich leiten von Positive Zahlen in negative umwandeln; die Frage und vor allem die bisherigen Antworten lassen mich vermuten, dass Antworten wie meine noch fehlen, um diese Diskussion sehr interessant zu gestalten. Wenn das nicht gewünscht ist, dann bitte sofort stoppen und ggf. meinen Beitrag löschen.
Jürgen
Hallo zusammen,
ich habe auch noch einen Vorschlag: 🙂 Ist natürlich der beste. 🙂
DateTime dt = DateTime.Today.AddDays(-1);
herbivore
Ha, warum denn selbst rechnen, wenn andere das viel besser - und täglich geprüft - können?
string url = "http://www.heise.de/newsticker/";
string search = "<h4>";
string response = "";
try
{
HttpWebRequest HeiseRequest = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse HeiseResponse = (HttpWebResponse)HeiseRequest.GetResponse())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(HeiseResponse.GetResponseStream()))
{
response = sr.ReadToEnd();
}
HeiseResponse.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return;
}
int positionVonDatum = response.IndexOf(search);
positionVonDatum = response.IndexOf(search, positionVonDatum + search.Length);
positionVonDatum = response.IndexOf(search, positionVonDatum + search.Length);
string Datum = response.Substring(positionVonDatum + search.Length, 10);
Console.WriteLine(Datum);
🙂
Xynratron
Herr, schmeiss Hirn vom Himmel - Autsch!
Die Erfahrung zeigt immer wieder, dass viele Probleme sich in Luft auslösen, wenn man sich den nötigen Abstand bzw. Schlaf gönnt.
public class HistoryTime
{
/// <summary> This structure represents a date and time. </summary>
public struct SYSTEMTIME
{
public ushort wYear, wMonth, wDayOfWeek, wDay,
wHour, wMinute, wSecond, wMilliseconds;
}
/// <summary>
/// This function retrieves the current system date
/// and time expressed in Coordinated Universal Time (UTC).
/// </summary>
/// <param name="lpSystemTime">[out] Pointer to a SYSTEMTIME structure to
/// receive the current system date and time.</param>
[DllImport("kernel32.dll")]
public extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
/// <summary>
/// This function sets the current system date
/// and time expressed in Coordinated Universal Time (UTC).
/// </summary>
/// <param name="lpSystemTime">[in] Pointer to a SYSTEMTIME structure that
/// contains the current system date and time.</param>
[DllImport("kernel32.dll")]
public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
public static DateTime GetYesterdayDateTime()
{
SYSTEMTIME t = new SYSTEMTIME();
GetSystemTime(ref t);
t.wDay--;
t.wDayOfWeek--;
SetSystemTime(ref t);
DateTime result = DateTime.Now;
t.wDay++;
t.wDayOfWeek++;
SetSystemTime(ref t);
return result;
}
}
Hallo zusammen,
ich habe die nicht so ganz ernst gemeinten Vorschläge mal ausgegraut.
herbivore