How can we get the date / time with the following format:
YYYY-MM-DD HH:MM:ss.mmm
In fact, I have to create a trace class to trace the communication flow between iOS device and another device. I need this format to have a good time stamp.
It should be:
NSString *MyString;
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd' 'HH:mm:ss.SSS"];
MyString = [dateFormatter stringFromDate:now];
Related
In Following Code I am trying to convert NSString to NSDate and once again converting to another Date format and sending it to server.
Following code worked perfectly fine when my Device Time Format is 12 hr. with dateformat to [dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"]; it gives me correct Date and Time.
But As soon as I changed device time Format to 24 hr. and change the datefomatter to [dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"]; it gives wrong Date and Time (Specifically time.) I had shown log values for code.
I had checked lot of stackoverflow questions regarding NSDateFormatter and Conversion of NSString to NSDate. But I am not able to find out where I'm making mistake and why it gives me wrong time when my Device time format is 24hr. Please help me to resolve this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);
Following log showing Date and Time When My Device Time format is 12hr. And it is Correct one.
From DateTime 2017-07-04T13:46:00
Time When My Device Time format is 24hr. And it is wrong (specifically time).
From DateTime 2017-07-04T12:46:00
Please try setting locale & timezone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"];
[dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: #"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);
I am doing a prayer alarm app and i want to compare my current time with fetched time, but when i am getting my current time, it has some time difference always as shown;
// getting today's date string
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatterToday = [[NSDateFormatter alloc] init];
dateFormatterToday.timeZone = [NSTimeZone localTimeZone];
[dateFormatterToday setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSString *currentDateTimeString = [dateFormatterToday stringFromDate:today];
// converting today's date string to NSDATE
NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
[dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm a"];
dateFormatterTodayFinal.timeZone = [NSTimeZone localTimeZone];
NSDate *dateTodayFinal = [dateFormatterTodayFinal dateFromString:currentDateTimeString];
Here currentDateTimeString, which is in string format showing my current time as :
2016-01-11 17:52 PM (which is correct one)
but dateTodayFinal, which is in Date format shows:
2016-01-11 07:22:00 +0000
I have tested with different timezones, but the issue persist, please help some one. Thank you.
The second example is missing a stringFromDate call so the description method is probably used which uses it's own format, not the dateFormatterToday formatter. Also missing are the printing calls so we can only guess.
Add:
NSString *dateTodayFinalTimeString = [dateFormatterToday stringFromDate:dateTodayFinal];
NSLog(#"dateTodayFinalTimeString: %#", dateTodayFinalTimeString);
Output:
dateTodayFinalTimeString: 2016-01-11 07:57 AM
NSDate doesn't have any information about the timeZone, when you hover over the NSDate in xCode it will show you the time is in UTC.
If you want to convert the time back and forth, this information (timezone) has to be in the string you want to parse as well and set the timezone back to UTC:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *currentDateTimeString = [dateFormatter stringFromDate:today];
// converting today's date string to NSDATE
//
// NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
// [dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
dateFormatterToday.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateTodayFinal = [dateFormatter dateFromString:currentDateTimeString];
Also have a look at the docs.
So i am getting a string containing date and time in this format "2014-12-22T11:00:00+0500" Now in order to convert it into NSdate i am using
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* date = [dateFormatter dateFromString:start_time];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString* temp = [dateFormatter stringFromDate:date];
self.eventDate = [dateFormatter dateFromString:temp];
NSDateFormatter* timeFormatter = [[NSDateFormatter alloc]init];
[timeFormatter setDateFormat:#"HH:mm:ss"];
NSString* temp2 = [timeFormatter stringFromDate:date];
self.start_time = [timeFormatter dateFromString:temp2];
Now even though the conversion is successful the problem is that eventDate also has has time after date 00:00:00. How can i remove this so that eventDate only contains date.
Conversly start_time has the time of event but also has some arbritrary reference date before that. How can i remove that so i only have time in start_time
I have searched hard and fast but haven't been able to figure out this problem. Any help would be appreciated.
You cannot remove either the date or the time to keep only one component. If I remember correctly NSDate object is internally just a number of seconds relative to a fixed point in time. So every NSDate contains the full date and time information.
What you probably want to do is to get the NSDateComponents you want from a NSDate object.
Instead of trying to store this separate, just display these dates separate. I think it could be useful sometimes to get the date completly, but i don't know your idea.
You can try with it, it may be help you.
NSString *finalDate = #"2014-12-22T11:00:00+0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [dateFormatter dateFromString:finalDate];
//For getting Time
NSDateFormatter* df1 = [[NSDateFormatter alloc]init];
[df1 setDateFormat:#"hh:mm:ss"];
NSString *time = [df1 stringFromDate:date];
NSLog(#"time %# ", time);
//For getting Date
NSDateFormatter* df2 = [[NSDateFormatter alloc]init];
[df2 setDateFormat:#"yyyy-MM-dd"];
NSString *actualDate = [df2 stringFromDate:date];
NSLog(#"actualDate %# ", actualDate);
It looks easy, but I couldn't figure out a proper way to do this. I need to create an NSString from a NSDate which represents the same time on every device, independently from the iPhone's time zone settings.
Suppose userA is in London, where the actual time is 14:00, userB is in New York where is 9:00 and userC is in Hong Kong, where the actual time is 21:00.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
Actually with this code I'm getting these results (when I log the dateString):
userA: 08/12/14 14:00:00
userB: 08/12/14 09:00:00
userC: 08/12/14 21:00:00
But I need to create dates like this
userA: 08/12/14 14:00:00
userB: 08/12/14 14:00:00
userC: 08/12/14 14:00:00
My goal is to create a "system/absolute time" which is the same inside the app and doesn't matter the original time zone of the user's device. The end result must look like this MM/dd/yy HH:mm:s.
Is it possible to get the NSDate *now = [[NSDate alloc] init]; from a pre-defined timezone? For example it could always use the actual time of the GMT-00 timezone.
I've tried to do it with this code, but when I run the code, the console writes out the wrong date (based on the device time zone setting) again, so I don't have a better idea. I would appreciate any ideas.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"Europe/London"];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"the date is %#,", dateString);
The below code should work. What ever the timezone you are in it will always display the time in UTC.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[[NSTimeZone alloc] initWithName:#"UTC"]];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"%#",dateString);
Between the following two threads, I think you'll find everything you need (and thensome). The first is an extensive example of a problem similar to yours (just remember to look at the code in the answers and not the question), while the second has all the time zone abbreviations that you'll ever need. Gotta love the helpful people on The Stack.
The links again, just in case
objective-c: Conversion between TimeZones
GMT timezone conversion in objective c
NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = #"HH:mm:ss";
NSString *dateString = [timeFormatter stringFromDate: localDate];
here is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:1275822000000+0000];
// the string is json parsing string
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(#"formattedDateString: %#", formattedDateString);
The output is Dec 20,5828963
But my required output is June 6,2010
How to change my code for correct output?
If you want to receive June 6, 2010 from some magic number then use this:
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:297475822];
For some reason your number is very big, and as #wrock mentioned it multiplies on 10 million, so for getting correct value you can use dateWithTimeIntervalSince1970 and divide it on 10 million
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1275822000];
First: your timestamp seems like a Java timestamp, but it has a few extra zeros (4 of them exactly). You have to convert this to a Unix timestamp, which is the number of seconds since the Unix epoch (as opposed to Java, which is the number of milliseconds since the Unix epoch).
dateWithTimeIntervalSinceReferenceDate: doesn't accept Unix timestamps, but the conversion is easy:
double timestampFromService = 12758220000000000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:
(timestampFromService*1.0e-7)];
Use date in readable manner :
NSDate *myDate = [NSDate dateWithString:#"2010-06-06 00:00:00"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:myDate];
NSLog(#"formattedDateString: %#", formattedDateString);