This question already has answers here:
Difference between System.DateTime.Now and System.DateTime.Today
(8 answers)
Closed 10 years ago.
These give me different dates
DateTime.Now.ToUniversalTime().ToString(#"yyyy-MM-dd");
DateTime.Today.ToUniversalTime().ToString(#"yyyy-MM-dd");
Why? I'm assuming it has something to do with the "time portion" of the datetime, perhaps set to 0-0-0.
The DateTime.Today property actually returns DateTime.Now.Date: And it's time segment is looks like 00:00.00000. And the DateTime.Now time segment is looks like 10:09.00000. So when you are converting to the ToUniversalTime it will depends on the current time.
public static DateTime Today {
get {
DateTime now = DateTime.Now;
return now.Date;
}
}
Because of ToUniversalTime().
From MSDN
The Coordinated Universal Time (UTC) is equal to the local time minus the UTC offset. For more information about the UTC offset, see TimeZone.GetUtcOffset. The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.
Related
This question already has answers here:
How to get the unix timestamp in C#
(17 answers)
Closed 8 months ago.
Can somebody help identify the format of this date and time (timestamp). THe webhook I am currently trying to have a link to has a security requirements where I need to get the timestamp. However, the format of the timestamp is new to me. See below the format:
The formatted timestamp converted to string should look like this:
1496734173
I have no idea how do I convert a date and time into something like this. I don't know what this code is or what time does it actually tells.
Click Here for the format
That looks like a pretty standard UNIX epoch timestamp. Assuming we're using the UTC (GMT) timezone, the date is Tuesday, June 6, 2017 7:29:33 AM.
UNIX time is the amount of seconds that have passed since Jan 1, 1970. The timestamp means 1496734173 seconds have passed since then, which is about 47 and a half years, i.e. June 6, 2017.
You can convert a DateTime object to a UNIX timestamp in the following way:
DateTime dateTime = DateTime.Now; // this would be your DateTime
DateTimeOffset offset = new DateTimeOffset(dateTime);
long epoch = offset.ToUnixTimeSeconds(); // our epoch is a 64 bit integer, i.e. long
Or, in one line:
long epoch = new DateTimeOffset(dateTime).ToUnixTimeSeconds();
I think that this represents the UNIX timestamp.
The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC.
Here you can try and convert your timestamp here: check your time
This question already has answers here:
How to get only time from date-time C# [closed]
(14 answers)
Closed 3 years ago.
How do I only store the time part in date time object? I am doing this:
DateTime.TryParseExact(DateTime.Now.ToString("HH:mm:ss"), "HH:mm:ss",
new CultureInfo("en-US"),
DateTimeStyles.None, out outTime);
Datetime LogTime = outTime;
But it still add date to it. I only want like "13:01:03" instead of "2019-10-31 13:01:03.000".
Time without a date is usually meaningless.
However, if you must store only the time, then TimeSpan would be appropriate. It offers some advantage over UInt64 by way of convenient methods of transforming milliseconds into other time units.
This will end up holding the milliseconds since "the beginning", where "the beginning" will have to be defined by you.
This question already has answers here:
How to get the current date without the time?
(14 answers)
Closed 3 years ago.
how can I get today datetime from 00:00, for example when I Use:
var dt=DateTime.Now; // 2019/1/1 15:22:22
I need Another extention method to give this string format:
string today = dt.TodayBegining(); // 2019/1/1 00:00:00
Just
DateTime.Today
Doc: DateTime.Today Property
An object that is set to today's date, with the time component set to 00:00:00.
This question already has answers here:
how to give format DateTime.Date?
(7 answers)
Closed 7 years ago.
I need only the date from a DateTime class so I use DateTime.Date,
but it gives me a Full DateTime format, like this:
DateTime date = new DateTime();
date.Date
give me:
+ date {12/01/2016 00:00:00} System.DateTime
I only need the:
date {12/01/2016}
12/01/2016 00:00:00 is the same as 12/01/2016 as a value.
If you wanna get 12/01/2016 as a string representation, you can use .ToString method like;
var str = date.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you wanna get this 12/01/2016 as a DateTime, simply you can't. A DateTime instance always has date and time part. That's why you see it as 12/01/2016 00:00:00 on debugger even if you set it's time part to midnight.
This question already has answers here:
DateTime difference in days on the basis of Date only
(3 answers)
Closed 8 years ago.
I'm using ASP.NET, C# and SQL Server. In the database the date is: 2014-10-28. Just year, month and day, no time portion.
I have this code:
DateTime data1 = new DateTime();
DateTime data2 = DateTime.Now;
data1 = reader.GetDateTime(3);
double total= (data2 - data1).TotalDays;
Response.Write(total.ToString());
The only problem of this is the output. The output of this is "4,81351624131366". Because probably this is the difference of the hours. Its possible to set the data2 to give me just the Y,M and D?
Or convert the total into days?
The property TotalDays returns the fractional portion of days too. Since the date you're comparing was 4 days ago at midnight, you're getting 4 full days and an additional 4/5th of a day (since it's not exactly midnight when you're executing this).
Either what Jon said (use DateTime.Today instead of DateTime.Now to get the date portion with a time of midnight), or use the Days property, which will just return a rounded 4 and drops the fractional portion.