The time is given as timestamp (in seconds). I need time only, so I take any date and add this time interval. For example:
NSDate *td = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"HH:mm";
dateFormatter.locale = #"en_GB";
NSString *result = [dateFormatter stringFromDate:d];
But it works incorrect: for 32400 it shows 12:00 (MSK) instead of 9:00.
How to solve this issue?
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
Related
I am trying to find end time with duration and start time using ColdFusion.
What I have tried is :
<cfset st_time=timeFormat("05:00:00",'hh:mm:ss tt')>
<cfset s_d=listToArray(duration,":")>
<cfset hours=s_d[1]>
<cfset min=s_d[2]>
<cfset sec=s_d[3]>
<cfset new_time = TimeFormat(createTime(hours, min, sec))>
Expected Output
start_time="05:00:00 PM"
duration="02:30:00"
then end_time should be 07:30:00 PM
how to calculate end time like this?
Step 1 - Covert your duration to seconds.
Step 2 - Use ColdFusion's DateAdd function to calculate the end time.
<cfscript>
// Assuming HH:nn:ss format for ALL times
function durationToSeconds (string duration){
if (listLen(arguments.duration, ":") EQ 3) {
var HH2ss = listGetAt(arguments.duration, 1, ":") *60*60; // or 3600 if you prefer
var nn2ss = listGetAt(arguments.duration, 2, ":") * 60;
return HH2ss+nn2ss+listGetAt(arguments.duration, 3, ":");
} else {
return "Ensure that your times are in HH:nn:ss format";
}
}
durationToSeconds("01:00:00"); // 3600
durationToSeconds("01:01:00"); // 3660
durationToSeconds("01:00:07"); // 3607
durationToSeconds("01:00"); // Ensure that your times are in HH:nn:ss format
startTime = "17:10:00";
duration = "02:20:30";
endTime = dateTimeFormat(dateAdd("s", durationToSeconds(duration), startTime), "HH:nn:ss"); //19:30:30
</cfscript>
To make it more understandable:
From Twitter API you will get String Date in this format (Thu Mar 26 11:51:30 +0000 2020)
All i want to do is make this String parse with DateTime format so i need to change the format from this (Thu Mar 26 11:51:30 +0000 2020) to something like this (26.04.2020 11:51:30), is this somehow possible ? Thank you :)
You can use substring method of string and cut each and every part then pass all that in datetime.
I hope Following code solve your issue.
int month;
String datetime = "Thu Mar 26 11:51:30 +0000 2020";
String monthInString = datetime.substring(4, 7);
int day = int.parse(datetime.substring(8, 10));
int hours = int.parse(datetime.substring(11, 13));
int min = int.parse(datetime.substring(14, 16));
int second = int.parse(datetime.substring(17, 19));
int year = int.parse(datetime.substring(26, 30));
DateTime _dateTime;
switch (monthInString) {
// add all months
case 'Jan':
month = 1;
break;
case 'Mar':
month = 3;
break;
}
_dateTime = DateTime.utc(year, month, day, hours, min, second);
print(_dateTime);
Using NSCalendar and its method dateByAddingComponents:toDate:options: the method sometimes return a wrong value. For instante:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2013];
[components setMonth:2];
[components setDay:27];
NSDate *date = [calendar dateFromComponents:components];
NSLog(#"%#", date); // 2013-02-27
NSDateComponents *addingComponents = [[NSDateComponents alloc] init];
[addingComponents setMonth:1];
[addingComponents setDay:4]; // value 5 give the same result.
date = [calendar dateByAddingComponents:addingComponents toDate:date options:0];
NSLog(#"%#", date); // 2013-03-31
The console log: 2013-02-27 and 2013-03-31.
Now, if a change the day component value to 5, the method return the same NSDate, but if a change to 6, the date if 2013-04-01.
Anyone has an answer for this behavior? Thanks on advance.
Rafael Cruz Márquez.
Say the hour is 15:03.
Say variable minutesToAdd is 39
What's the code that will display 15:42 on a UILabel (current time + minutesToAdd)?
Here is some code, I dont check it in XCode (and its will not compiled just after copy/paste), but the idea is like I wrote below:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dc = [[[NSDateComponents alloc] init] autorelease];
dc.minute = 39;
NSDate *srcDate = NSDate(15:03); // Create date here
NSDate *resultDate = [calendar dateByAddingComponents:dc toDate:srcDate]; // Here is your new date
I have a dropdownlist that displays time. For example 8:00AM or 8:30AM.
When I save this time to database, I want to save as todays date + time.
eg: 8:00AM as 03/30/2009 8:00:00:000. Can anybody give appropriate code to convert as shown above?
I tried
Convert.ToDateTime(ddlStartTime.SelectedItem.Text)
But there is an error stating "String was not recognized as a valid DateTime."
VB.NET answer for Portmans Solution. Too many chars for comment so included here.
Dim time As String() = Me.DropDownList1.SelectedValue.Split(New Char() {":", " "})
Dim hours As Integer = Integer.Parse(time(0))
Dim minutes As Integer = Integer.Parse(time(1))
Dim ampm As Integer = 12
If time(2).ToLower() = "am" Then
ampm = 0
End If
Dim dt As DateTime = DateTime.Today.AddHours(hours + ampm).AddMinutes(minutes)
Have a look at DateTime.TryParse and DateTime.Today. Using them should be enough to do what you want.
Untested Code.
DateTime dt;
if (DateTime.TryParse(Dropdown1.SelectedValue, out dt))
{
DateTime result = DateTime.Today.AddHours(dt.Hour).AddMinutes(dt.Minute);
}
Basically, you just need to parse the time string. It will be automatically resolved to the current date.
Dim strTime As String = "8.30am"
Dim parsedTime As DateTime
If DateTime.TryParseExact(strTime, "h.mmtt", New System.Globalization.DateTimeFormatInfo(), Globalization.DateTimeStyles.None, parsedTime) = True Then
'Parse was successful.
Else
'Handle the error.
End If
Store as the value for each drop down item the number of minutes from midnight that the time represents. Then:-
valueToStore = DateTime.Today + TimeSpan.FromMinutes(Int32.Parse(value))
By storing using the value attribute of a HTML option to store a simple representation of the value you eliminate the codes dependancy on the actual format used to simply display the set of values. If it decided that the representation of the times be changed to use different format the rest of the code will continue to work unmodified.
var time = this.DropDownList1.SelectedValue.Split(':', ' ');
var hours = Int32.Parse(time[0]);
var minutes = Int32.Parse(time[1]);
var ampm = (time[2] == "PM") ? 12 : 0;
var dt = DateTime.Today.AddHours(hours + ampm).AddMinutes(minutes);
Parse your DropDownList for Hours and Minutes, then add them to DateTime.Today.
Read the parts of the time so you have hours and minutes, then use the following code:
DateTime myDate = DateTime.Now.Date;
myDate = myDate.AddHours(hours);
myDate = myDate.AddMinutes(minutes);
Use ParseExact so that you can specify the format that you are using.
h = hours in 12-hour clock format
. = literal character
mm = minutes as two digits
tt = AM/PM designator
Dim time As DateTime = DateTime.ParseExact(dropdown.SelectedValue, "h.mmtt", CultureInfo.InvariantCulture)
The components of the DateTime value that you don't specify in the string (year, month, date, seconds, time zone, era) uses DateTime.Today as default, which is exactly what you want in this case.
This always seems to work for me. May not be the most elegant way, but I have not had any issues so far.
dTime = DateTime.Now;
string time = this.DropDownList1.SelectedValue;
DateTime FormattedDateTime = Convert.ToDateTime(dTime.Date.ToShortDateString() +
" " + time);