how to convert Javascript Date to iOS date format - ios

I am new in iOS development. Actually I am trying to show some information which I get from a web service in a table view.
I have successfully retrieved the response as JSON in my code, but in my table view there is a label to show date.
But in my response the date is something like this
/Date(1391068800000)/
How can I convert this to show in my table view? I think this is Javascript date.
But I'm not sure how to convert it.

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[your timestamp doublevalue]/1000];
divide value by 1000 cause of milliseconds (13 digit)

The date in your response is a timestamp. You can create a date object by:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1391068800000];
After that you can convert this date to a string in an appropriate format by using the NSDateFormatter class. For example:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateString = [formatter stringFromDate:date];
For more information about formatting dates see: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

Related

nil after converting string to date [duplicate]

Maybe somebody can help explain why I am getting a null value when converting a string to a date. It all looks right but I'm obviously missing something here.
Some background:
This iPad app will be used in different countries and I will need to do a calculation on the date to see if 90 days have passed since a user last logged in.
I have a SQLite Database with a DateLastIn field set as TEXT
My Object has a DateLastIn property set as NSDate
When populating my record object I set up a NSDateFormatter as such..
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; // SQLite default date format
Then I read in the DateLastIn (Using FMDB wrapper for SQLite).
// test SQLite as String
NSString *testDate = [results stringForColumn:#"DateLastIn"];
NSLog(#"DateLastIn straight from DB (string) shows %#", testDate);
Result:
DateLastIn straight from DB (string) shows 2012-04-23 18:20:51
All is good so far. Next I test converting this to an NSDate object e.g
NSDate *aDate = [[NSDate alloc] init];
aDate = [formatter dateFromString:testDate];
NSLog(#"Using formmater on string date results in: %#", aDate);
Result:
Using formmater on string date results in: (null)
I have tried DATETIME in SQLite, I've tried using NSString in my object instead of NSDate and seem to be going around in circles.
Any help much appreciated.
NSDateFormatter uses the format patterns from the Unicode Technical Standard #35.
For the hour format, you need HH (Hour [0-23]) not hh (Hour [1-12]).
I changed your date format to HH not hh and it works. Here is my test code....
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // SQLite default date format
// test SQLite as String
NSString *testDate = #"2012-04-23 18:20:51";
NSDate *aDate = [formatter dateFromString:testDate];
NSLog(#"Using formmater on string date results in: %#", aDate);

converting NSDate from NSString [duplicate]

Maybe somebody can help explain why I am getting a null value when converting a string to a date. It all looks right but I'm obviously missing something here.
Some background:
This iPad app will be used in different countries and I will need to do a calculation on the date to see if 90 days have passed since a user last logged in.
I have a SQLite Database with a DateLastIn field set as TEXT
My Object has a DateLastIn property set as NSDate
When populating my record object I set up a NSDateFormatter as such..
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; // SQLite default date format
Then I read in the DateLastIn (Using FMDB wrapper for SQLite).
// test SQLite as String
NSString *testDate = [results stringForColumn:#"DateLastIn"];
NSLog(#"DateLastIn straight from DB (string) shows %#", testDate);
Result:
DateLastIn straight from DB (string) shows 2012-04-23 18:20:51
All is good so far. Next I test converting this to an NSDate object e.g
NSDate *aDate = [[NSDate alloc] init];
aDate = [formatter dateFromString:testDate];
NSLog(#"Using formmater on string date results in: %#", aDate);
Result:
Using formmater on string date results in: (null)
I have tried DATETIME in SQLite, I've tried using NSString in my object instead of NSDate and seem to be going around in circles.
Any help much appreciated.
NSDateFormatter uses the format patterns from the Unicode Technical Standard #35.
For the hour format, you need HH (Hour [0-23]) not hh (Hour [1-12]).
I changed your date format to HH not hh and it works. Here is my test code....
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // SQLite default date format
// test SQLite as String
NSString *testDate = #"2012-04-23 18:20:51";
NSDate *aDate = [formatter dateFromString:testDate];
NSLog(#"Using formmater on string date results in: %#", aDate);

Data sorting according to Date

I have some data which contains sold date. I want to sort them according to Week, Month, Year. For eg. If I select week then results returned should be within 7 days of current date. Same goes for Month and Year.
The relevant field from the data which is fetched from a web service looks like the following:
AuctionStartTime = "05/03/2016 09:30:00 AM"
You have use NSDateFormatter to convert the string to your date format and using NSDateFormat to get required date values and perform your task. The set date format using following code...
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"Your Date Format"];
Convert string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
The your date format to set your required format. For more details of date format to click now.
You shoud use a NSDateFormatter to convert the NSString date to NSDate then you can compare that to current date. After that you can easily pick the last week's, month's or year's dates.

Convert a date to English format form Bengali

I have a date picker in my app. The phone is set to Bangladesh local settings. When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.
Like, it returns ০৬/১১/২০১৪
but I want it to be 06/11/2014.
I've tried converting it by date formatter. This is what I tried:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
[formater setDateFormat:#"dd/MM/yyyy"];
NSLog(#"%#",[formater stringFromDate:date]);
The output is null.
You are incorrect in your assumption when you say...
When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.
UIDatePicker returns an NSDate object. NSDate has no formatting at all. It has no language, it is purely a point in time.
When you do this...
NSLog(#"%#", someDate);
The system will render that point in time into a string and then print it. It is the rendering into a string that contains the format.
I'm guessing what you are doing is this...
Get a date from a UIDatePicker.
Render that date into a UITextField in Bengali. (or label, or text view or something)
Trying to read the text and store it into a date.
Trying to then "convert" the date to an English string.
What you should be doing is just saving the date that comes from the date picker.
Put it into a property or something.
In your above code I think the bit that is failing is actually the first bit...
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];
Because you're not giving it a format it will fail. But this is the wrong way to go about it anyway.
You should have something like this...
- (void)datePickerChoseADate
{
self.date = self.datePicker.date;
}

iOS - Converting time and date to user time zone

I am sending some requests on a webserver that replies me time and date like this:
"at 18:58 of 05/08/2012"
I can figure out how to get the time and the date in 2 NSStrings(18:58, 05/08/2012).
Note that the server's time zone is +00:00. What I want to accomplish is to present this time based on user's location. So for example if the reply from server is 23:30 at 05/08/2012 and the user's time zone is +2:00 I want to present him 1:30 at 06/08/2012.
Any ideas?
You should do it the following way:
1) First, create an NSDateFormatter to get the NSDate sent from the server:
NSDateFormatter *serverFormatter = [[NSDateFormatter alloc] init];
[serverFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[serverFormatter setDateFormat:#"'at 'HH:mm' of 'dd/MM/yyyy"];
From Apple docs: note with the Unicode format string format, you should enclose literal text in the format string between apostrophes ('').
2) Convert the string (consider it is defined as theString) to a NSDate:
NSDate *theDate = [serverFormatter dateFromString:theString];
3) Create an NSDateFormatter to convert theDate to the user:
NSDateFormatter *userFormatter = [[NSDateFormatter alloc] init];
[userFormatter setDateFormat:#"HH:mm dd/MM/yyyy"];
[userFormatter setTimeZone:[NSTimeZone localTimeZone]];
3) Get the string from the userFormatter:
NSString *dateConverted = [userFormatter stringFromDate:theDate];
instead of getting absolute time from server get timestamp from server and convert that in to date at the client side, for this you didn't have to change the timezone also.

Resources