简体   繁体   中英

convert minutes to date and time c#

I have this minutes number 2400 . I need convert 2400 minutes to exact date

Like this 2400 minutes equal ==> 2012-12-17 04:00:00

A number of minutes is a time interval , not an absolute date . You need to know what does the number of minutes represent, ie the number of minutes since what event, in order to get an absolute date.

Assuming it's the number of minutes since the beginning of, say, a specific year, you can do it like this:

var start = new DateTime(2012, 1, 1);
var interval = new TimeSpan(0, 2400, 0);
var date = start + interval;

I'm guessing you look for this:

        int minutes = 2400;
        var dt = DateTime.Now.Date.AddMinutes(minutes);

If you write,

DateTime d = DateTime.Today.AddMinutes(2400);
Console.WriteLine(d);

Output:

11/30/2016 4:00:00 PM

But if the current time is 11/30/2016 7:00:00 AM and if you wrilte,

DateTime d = DateTime.Now.AddMinutes(2400);
Console.WriteLine(d);

Then output will be:

11/30/2016 11:00:00 PM

You can use the AddMinutes function of the DateTime class:

    DateTime dt = DateTime.Today;
    MessageBox.Show(dt.AddMinutes(2400).ToString());
DateTime date = new DateTime(2010, 1, 1); // the start date, example 2010 jan 1

DateTime newDate =  date.AddMinutes(2400); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM