number formatter : I need 01 not 1 [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I pad an integer on the left with zeros?
In my iPhone app I am calculating time in this manner.
int timeTakeInSeconds = (int)(self.timeTaken*[constants timer]);
int timeTakenMins= (int)(timeTakeInSeconds/60);
int timeTakenSecs =(int)(timeTakeInSeconds%60);
NSString *timeText = [NSString stringWithFormat:#"Time %d:%d",timeTakenMins,timeTakenSecs];
By this I am getting second as 1,2,...10, 11..and so on. But I need as 01, 02, 03...10, 11, 12..and so on..How can I do this? Is there any way to do this by number formatter? Please help.

Try using %d:%02d instead of just %d:%d. This is using the standard C-style printf formatting options. The documentation can be found at Format Specifiers.

Related

Convert date and time in dart [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
How do I format a date with Dart?
(24 answers)
Closed 9 months ago.
from API I am getting this:
"2022-05-12T15:55:03.000000Z"
My question is, how can I make it:
12.05.2022, 15:55
My question is, is here any easy way? Otherwise I can slowly and partially convert it with string methods, but I guess there must be better, more proper way?
Thank you in advance
you can user DateFormat to get a date as string formatted the way you want
String x = "2022-05-12T15:55:03.000000Z";
DateTime y = DateTime.parse(x);
final DateFormat formatter = DateFormat('dd.MM.yyyy, HH:mm');
final String formatted = formatter.format(y);
print(formatted); // 12.05.2022, 15:55

How to work with date in Unity c#? [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 4 years ago.
I create a game in unity. And I add a 'buy subscribe' (apple).
After click buy, I get Date (for example: 08/02/2018 07:24:23) 08 - is month, 02 - is date.
I get a date through this code:
productReceipt.subscriptionExpirationDate
Now i need to do a check, if CurrentDay - 08/02/2018 07:24:23 = 15 days;
How to calculate 2 dates?
You can do it using DateTime function. DateTime function allows you to calculate the number of days. You just need to get current datetime & parse your string to datetime format. Here is the code.
DateTime dateCurrent = DateTime.Now;
DateTime dateNext = DateTime.Parse(date);
int days = (dateNext - dateCurrent).Days;
Update:
You can also use TotalDays function to give the remaining time in fraction also. TotalDays is double so remember to change the days to double when using it.

Swift: Trunc a floating number to show it in a label [duplicate]

This question already has answers here:
String formatting of a Double [duplicate]
(2 answers)
Closed 8 years ago.
When I wanted to trunc a floating number for showing in a label in Objective-C I could just use LABEL.text = [NSString stringWithFormat:#"%.3f", FLOAT];
Though I can't find how to do so in Swift.
The old NSString format is still available. You can use that.
let myfloat : Float = 1.23
LABEL.text = NSString(format: "%.3f", myfloat)

Separate part of string from whole string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective C: How to extract part of a String (e.g. start with '#')
I want to divide string in to few parts. My string is:
NSString * myString = #"Multiply top and bottom to get < mathtype x3y2z3 over x3yz2 >,now cancel down the powers of < i >x, y, z< /i >.If you prefer, you can cancel down before you start multiplying.";
I need three parts from this.
1. Multiply top and bottom to get
2. < mathtype x3y2z3 over x3yz2 >
3. ,now cancel down the powers of < i >x, y, z< /i >.If you prefer, you can cancel down before you start multiplying.
How can I do this?
If the above fails you could always convert it to an array of characters and loop through it.
- (const char *)cStringUsingEncoding:(NSStringEncoding)encoding
there are more details in the NSString Docs

Given Year, Month,Day and the Week number, is it possible to get the Date in C#? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calculate date from week number
Given Year, Month,Day and the Week number, is it possible to get the Date?
e.g. Year = 2010
Month =Jan
Day = Sun
WeekNumber = 3
output : 2010-01-10
I am trying it in c#
Thanks
I would make it like this:
int Year = 2010;
int Month = 1; //Jan=1, Feb=2, ...
int Day = 0; //Sun=0, Mon=1, ...
int WeekNumber = 3; // greater than 0
DateTime dateValue = new DateTime(Year, Month, 1);
int firstDay = (int)dateValue.DayOfWeek;
dateValue = dateValue.AddDays(Day - firstDay + (WeekNumber - 1) * 7);
I don't think there's something for such date calculations in plain .NET BCL. But there are libraries that can help you, see i.e. Fluent DateTime. Using this library, you can try something like that:
var firstWeekInYearBeginning = new DateTime(2010, 1, 2).Previous(DayOfWeek.Monday); // note 2 to not miss a week if the year begins on Monday
var expectedDate = 3.Weeks().From(firstWeekInYearBeginning);
Based on the APIs here, don't this its possible to initialize a DateTime Object from the information given. You would need to develop an algorithm to get the exact date of the year. A simple strategy would be to get the first Day of the month and based on that, find first Monday of the month. This is the start of WeekNumber 1 for that month, and you can locate your required Week by simpl loop and locate the exact date. You would then know the calendar date you are interested in.
BTW: I am assuming WeekNumber of a year/month starts from the first Monday of that Year/Month. Someone please correct me if I am wrong.
Maybe you should check out System.Globalization.Calendar class. It might be useful.