-2

I'm Using NSOutputStream to output data from the client (IOS device) To the server (PC Device). In the following code I'm Outputting some data using outputStream and(this code is working 100% )

 NSString *response  = @" Today's Sunny  \n  ";
 NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
 [outputStream write:[data bytes] maxLength:[data length]]; 

I would like to do the same thing using Outputstream to output the current time like (09:24.00) in this format (hh:mm:ss)

Here what i have tried

  NSDate *currentTime = [NSDate date]; 
  NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  [formatter setDateFormat:@"hh:mm:ss"];
  //outputstream here 

I would really appreciate your help in this matter

Assam AlZookery
  • 479
  • 4
  • 15

4 Answers4

1

You need to convert the date to a string and then add it to your output stream.

Use:

NSDate *currentTime = [NSDate date]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"hh:mm:ss"];
NSString *timeString = [formatter stringFromDate:currentTime];

You can then send timeString in your output stream

Adam Richardson
  • 2,518
  • 1
  • 27
  • 31
  • DO you have any knolwge to solve this question for me http://stackoverflow.com/questions/37141410/ios-how-to-reconnect-to-the-server-pc-after-its-got-disconnected – Assam AlZookery May 10 '16 at 15:27
1

Take a look at this, it has everything you need about date programming on iOS http://rypress.com/tutorials/objective-c/data-types/dates

Updated with some code samples:

Converting current time from NSDate to NSString:

NSDate *currentTime = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setCalendar:[NSCalendar currentCalendar]];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[dateFormatter setDateFormat:@"M.d.y"];

NSString *stringResult = [dateFormatter stringFromDate:currentTime];

Setting Calendar and TimeZone is optional, but if could use them if needed. You also have different optionals for them, like NSCalendarIdentifierGregorian, timeZoneForSecondsFromGMT:, etc.

You have many options for dateFormat, for example

M/d/y --> 11/4/2012

MM/dd/yy --> 11/04/12

MMM d, ''yy --> Nov 4, '12

h:mm a --> 8:09 PM

Alternatively, you can instead use dateStyle and timeStyle

[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
Community
  • 1
  • 1
Stephenye
  • 806
  • 6
  • 12
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/12294505) – Eiko May 09 '16 at 18:45
  • @Eiko alright. updated with some code samples and explanation – Stephenye May 09 '16 at 19:19
0

If someone need the code of Assam + Adam in Swift, here it is :

var currentTime:NSDate  = NSDate.date()

var formatter:NSDateFormatter  = NSDateFormatter.alloc().init()
formatter.setDateFormat("hh:mm:ss")

var timeString:NSString  = formatter.stringFromDate(currentTime)
outputStream.write(timeString.UTF8String(),maxLength:timeString.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
Ciboulette
  • 145
  • 1
  • 2
  • 9
-2

Here's the Complete answer, Thanks to @Adam

NSDate *currentTime = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"hh:mm:ss"];
    NSString *timeString = [formatter stringFromDate:currentTime];
    [outputStream write:[timeString UTF8String]
              maxLength:[timeString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
Assam AlZookery
  • 479
  • 4
  • 15