How to Get the End of a Day

How to get start and end of day in Javascript?

var start = new Date();
start.setUTCHours(0,0,0,0);

var end = new Date();
end.setUTCHours(23,59,59,999);

alert( start.toUTCString() + ':' + end.toUTCString() );

If you need to get the UTC time from those, you can use UTC().

How to get the end of a day?

Take the date, truncate it, add one day and subtract one second:

select date_trunc('day', date) + interval '1 day' - interval '1 second'

You can put the logic in an update if you want to change the data in the table.

Of course, you can also add 24*60*60 - 1 seconds:

select date_trunc('day', date) + (24*60*60 - 1) * interval '1 second'

But that seems less elegant.

How to get the beginning and end of the day with moment?

It is giving you midnight local time, but you're printing it out in zulu time. Try using toString instead, it will print the time out in local time.

now = moment()console.log('now ' + now.toString())console.log('start ' + now.startOf('day').toString())console.log('end ' + now.endOf('day').toString())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>

Is this the proper way to get the end of day in PHP?

Можно и так

$begin = date('Y-m-d 00:00:00');

$end = date('Y-m-d 23:59:59',strtotime($date_to_change));

How to get the end of day as a OffsetDateTime in the format 2019-12-29 05:59:59.9999990 +00:00?

An OffsetDateTime does not have any format itself, it holds the information about the date and the time. If you create an OffsetDateTime just from a date it will get the default time information of 0 hours, minutes, seconds and nanos.

You can output it in various formats using a DateTimeFormatter and create a new instance of OffsetDateTime adding temporal units to an existing one like this:

public static void main(String[] args) {
// example OffsetDateTime
OffsetDateTime offsetEndDt = OffsetDateTime.of(2020, 3, 14, 0, 0, 0, 0,
ZoneOffset.UTC);
// define a formatter for the output
DateTimeFormatter myFormatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnnnnn xxx");
// print it using the previously defined formatter
System.out.println(offsetEndDt.format(myFormatter));
// create a new OffsetDateTime with time information
OffsetDateTime realEndOfDay = offsetEndDt
.withHour(23)
.withMinute(59)
.withSecond(59)
.withNano(999999000);
// print that, too
System.out.println(realEndOfDay.format(myFormatter));
}

It produces the following output using the pattern your example desired output has:

2020-03-14T00:00:00.000000000 +00:00
2020-03-14T23:59:59.999999000 +00:00

How to get next day end of day date time from given Instant ? (Java)

One way is converting Instant to ZonedDateTime with UTC timezone and modify the date as per requirement and then convert it back

Midday :

Instant result = instant.atOffset(ZoneOffset.UTC)
.plusDays(1).with(LocalTime.of(11,59,59,instant.getNano()))
.toInstant();

End of day :

Instant result = instant.atOffset(ZoneOffset.UTC)
.plusDays(1).with(LocalTime.of(23,59,59,instant.getNano()))
.toInstant();

How to obtain the end of the day when given a LocalDate?

Here are a few alternatives, depending on what you need:

LocalDate.now().atTime(23, 59, 59);     //23:59:59
LocalDate.now().atTime(LocalTime.MAX); //23:59:59.999999999

But there is no built-in method.

As commented by @JBNizet, if you want to create an interval, you can also use an interval up to midnight, exclusive.

How to calculate start of day and end of day from a date in c#

Start of day is easy; you can just use .Date.

FYI: Please make sure you check culture.

Sample Image

class Program
{
static void Main(string[] args)
{

DateTime date;
if (DateTime.TryParse("01/15/2015", out date))
{
var startOfDay = date.Date;
Console.WriteLine(startOfDay.ToString("s") + "Z");

var endOfDay = date.ToEndOfDay();
Console.WriteLine(endOfDay.ToString("s") + "Z");
}

Console.ReadLine();
}
}

public static class DateExtensions
{
public static DateTime ToEndOfDay(this DateTime date)
{
return date.Date.AddDays(1).AddTicks(-1);
}
}

How to get the start and end times of a day

If you are only worried about .Net precision...

startDate = DateTime.ParseExact(dates[0], "yyyyMMdd");
endDate = DateTime.ParseExact(dates[1], "yyyyMMdd").AddTicks(-1).AddDays(1);

You really don't need to concatenate extra values onto the string for the time portion.


As an addendum, if you are using this for a query against, for example, a database...

startDate = DateTime.ParseExact(dates[0], "yyyyMMdd");
endDate = DateTime.ParseExact(dates[1], "yyyyMMdd").AddDays(1);

With a query of...

WHERE "startDate" >= @startDate AND "endDate" < @endDate

Then the precision issues noted in the comments won't really matter. The endDate in this case would not be part of the range, but the outside boundary.



Related Topics



Leave a reply



Submit