converting datetime format to time with only hour and minute - datetime

I have values in datetime format. ex: 2012-10-17 00:05:00.000
I casted them into time format ex: 00:05:00.000 but it is still too long for me. I just want hour and minute part of that time. Does anyone know?

Try this:
var dateTime = new DateTime(2012, 10, 17, 0, 5, 0, 0);
var hoursAndMinutes = dateTime.ToShortTimeString();

Related

How to get last 30 minutes,1 Hour time from the from current time in DART

how can I get the last 3 hours time from the current time in DART
I am getting the current time by this
DateTime toTime = new DateTime.now();
I want to get last last 30minutes before,lastOneHour,lastThreeHour,last 6,last 12,last 24 time from the current time.
I tried doing like this
from = toTime.subtract(new Duration(hours: 0.5)).millisecondsSinceEpoch;
//This is not working
I am converting those result to epochtime.
As #julemand101 pointed out, Duration has its named hours parameter as an int. Passing a double will toss an error. Instead, write your code something like this:
DateTime curTime = new DateTime.now(); //Current local time
DateTime thirtyMinAgo = curTime.subtract(Duration(minutes: 30));
int thirtyMinAgoInMs = thirtyMinAgo.millisecondsSinceEpoch;
return thirtyMinAgoInMs;
Try out this package, Jiffy. Inspired by momentjs. It also takes into account the leap years and how many days there are in each month
To get the last 30 minutes
Jiffy().subtract(minutes: 30);
// You can also add your DateTime object
Jiffy(DateTime.now()).subtract(minutes: 30);
To get the last 3 hours
Jiffy().subtract(hours: 3);
You can also get up to months and years
var jiffy = Jiffy()
.subtract(minutes: 30, hours: 3, days: 6, months: 2, years: 1);
print(jiffy.dateTime);
// Or you can format it on the go
print(jiffy.format("dd, MMM yyyy"));
print(jiffy.yMMMd);
Hope this helped.

When i get the difference between endDate.millisecondsSinceEpoch and startDate.millisecondsSinceEpoch it gives me the result plus 2 hours?

I want to get the difference between endDate and startDate
int endDate = DateTime.parse("2019-01-31 09:35:00").millisecondsSinceEpoch;
int startDate = DateTime.parse("2019-01-31 09:30:00").millisecondsSinceEpoch;
Then i get the diffrernce
int distance = endDate - startDate;
After that i convert distance to DateTime
DateTime newTime = DateTime.fromMillisecondsSinceEpoch(distance);
Result
02:05:00
Expected Output
00:05:00
Where is the mistake ?
Date/Time calculations in local timezone are prone to mistakes.
Convert to UTC first:
int endDate =
DateTime.parse("2019-01-31 09:35:00").toUtc().millisecondsSinceEpoch;
int startDate =
DateTime.parse("2019-01-31 09:30:00").toUtc().millisecondsSinceEpoch;
int distance = endDate - startDate;
DateTime newTime = DateTime.fromMillisecondsSinceEpoch(distance, isUtc: true);
print('result: $newTime');
result: 1970-01-01 00:05:00.000Z
But as mentioned in the comment below your question, Duration would be better for that purpose.
Try this very simple package, Jiffy that is inspired by momentjs
Looks like you are trying to get the difference in minutes. See below
var startDate = Jiffy(DateTime(2019, 1, 31, 9, 35));
var endDate = Jiffy(DateTime(2019, 1, 31, 9, 30));
print(startDate.diff(endDate, Units.MINUTE)); // 5 minutes
You can also get the difference in the following units, years, months, weeks, days, hours, minutes, seconds and milliseconds. Also, see below
print(startDate.diff(endDate, Units.SECOND)); // 300 seconds
print(startDate.diff(endDate, Units.MILLISECOND)); // 300000 milliseconds

asp.net how to combine two datetime date to one datetime object?

I have
Datetime starttime='1/1/1900 6:00:00PM'
Datetime ExamDate='12/9/2013 12:00:00PM'
i want to combine these such that the result
Datetime combine=starttime+ExamDate;
so,
Result Datetime combine='12/9/2013 6:00:00PM'
Please help thank you
I'm not 100% certain if this is what you're trying to achieve, but I'll give it a shot. To add time to an existing DateTime object, use the TimeSpan class. Ex:
TimeSpan StartTime = new TimeSpan(0, 6, 0, 0); //new time span of 6 hours
// create date time 2013-09-12 12:00
DateTime ExamDate = new DateTime(2013, 9, 12, 12);
//Add the 6 hour time span to your exam date to get combined date
DateTime Combined = ExamDate + StartTime;
If you'd simply like to add the TIME portion of starttime to examDate
var startTime= DateTime.Parse("1/1/1900 6:00:00PM");
var examDate = DateTime.Parse("12/9/2013 12:00:00PM");
var result = examTime.Add(starttime.TimeOfDay);
This will result in: 13/9/2013 6:00:00AM.
It's occurred to me you might just be trying to store the date in examDate, in which case setting examDate to 12/9/2013 12:00:00AM will give: 12/9/2013 6:00:00PM.

ASP.NET: Date format

I am using following code to format my datum
ToString("MMMM dd, hh:mmtt", System.Globalization.CultureInfo.CreateSpecificCulture("en-UK")
It results in i.e. June 01, 8:34PM. I need only "PM" ("AM") be lowercase "pm" ("am"). What is the simplest method to achieve it?
Thanks!
I think you're going to have to split the strings and join them together.
Also, you don't need to specify the culture if you're doing custom formatting. The culture is really for the standard formats.
So I would do something like the following
var curDate = DateTime.Now;
var dateString = curDate.ToString("MMMM dd, hh:mm") + curDate.ToString("tt").ToLower();
Try this
DateTime dt = new DateTime(2013, 3, 9, 16, 5, 7, 123); // put your datetime variable value at last paramter value.
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
For lowercase you just need to use .ToLower().
Or you can find more information HERE

Flex: How to get the number of Days in a particular month in Flex?

I am having a problem with flex.
How can I get the number of Days in a particular month in Flex?
Thanks
Use the Date object , setting the day to 0, getDate will return the last day in the month which is also the day count; you have also to give the year you want to check, because you know february can have 29 days.
function getDayCount(year:int, month:int):int{
var d:Date=new Date(year, month, 0);
return d.getDate();
}
trace(getDayCount(2012,2));
The example above doesn't work for January!
new Date(2011,0,0) returns 12/31/2010
new Date(2011, 1, 0) returns 01/31/2011
new Date(2011, 1, 1) returns 02/01/2011
Create a new date object by specifying the year, desired month + 1, and a day of 0. This will create a date object for the last day of the desired month. Then call getDate() on the object to return the last day.
Note that months are zero based in Flex, so Jan = 0, Feb = 1, and so on. Therefore, if you want to know what the last day in Feb was for 2012 you would do the following:
var FEB:int = 1;
var date:Date = new Date(2012, FEB + 1, 0);
var lastDayInFeb:Number = date.getDate();
Here is a more complete example with a couple of non-unit tests and a reusable static method for returning the last day of a month.

Resources