Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:01:07+00:00 2026-06-17T05:01:07+00:00

I have a sqlite3 Database with a DATETIME column containing values formatted like this:

  • 0

I have a sqlite3 Database with a DATETIME column containing values formatted like this:
2013-01-09 04:00:00

I’m trying to figure out how to determine if times are AM or PM?

Here’s how I read and convert the DATETIME objects from my Sqlite3 DB using Objective-C:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
char *DateTimeColumn = (char *)sqlite3_column_text(statement, 13);
NSDate *myDate =[dateFormat dateFromString:[NSString stringWithUTF8String:DateTimeColumn]];

This works really well except it doesn’t figure out whether this is 4:00AM or 4:00PM.

I saw a posting suggesting adding an a at the end of the formatter string like so:
"yyyy-MM-dd hh:mm:ss a"
… to figure out AM/PM – but that caused crashes. (I of course also added AM and PM to the actual values in my DATETIME column like so:
2013-01-09 04:00:00 AM

but its still crashed.

Not sure if I’m doing something wrong – or if this just can’t be done.

Is the only solution to have the entries in the DB itself all be in the 24hr. format (meaning: 4:00PM should be entered as 16:00) – and then use IF statements to subtract 12 from values > 12 so as to determine a time’s AM or PM’ness?
I would have thought there’d be some automated function for this… Any ideas?

=================================

EDIT/UPDATE:
I’m suddenly getting a ‘(null)’ after converting the string into an NSDate. Here’s the current version of the code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
char *DateTimeColumn = (char *)sqlite3_column_text(statement, 13);
NSString *DateTimeString = [NSString stringWithUTF8String:DateTimeColumn];
NSLog(@"DateTimeString = %@", DateTimeString);
   // This NSLog gives me a good string: "DateTimeString = 2013-01-09 16:00:00"
// But when I create an NSDate object from this string:
NSDate *myDate =[dateFormat dateFromString:DateTimeString];
// ... and log it out: 
NSLog(@"the REAL DATETIME object = %@", [myDate description]);
// the console shows: "the REAL DATETIME object = (null)"
// and when I add this NSDate object to my Array of NSDates, I crash:
[tempNSDateObjectsArray addObject:myDate];

Console shows: 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',     
reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:01:08+00:00Added an answer on June 17, 2026 at 5:01 am

    HH, not hh, is for the 24-hour date format:

    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    

    Parsing “2013-01-09 16:00:00” with hh as hour-format fails and therefore returns nil.

    Note that NSDateFormatter interprets the date string according to the local timezone, unless you set an explicit time zone. You might want to add the time zone information to the strings, or use different format (e.g. UNIX time, seconds since 1.1.1970) to store the date.


    ADDED: The SQLite datetime() function returns a string “YYYY-MM-DD HH:MM:SS” which is the date and time in UTC/GMT. To convert such a string to a NSDate, use a NSDateFormatter and set the time zone to GMT:

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    
    NSString *dateTimeString = @"2013-01-09 16:00:00";
    NSLog(@"DateTimeString = %@", dateTimeString);
    
    NSDate *myDate =[dateFormat dateFromString:dateTimeString];
    NSLog(@"myDate:          %@", myDate);
    

    Output:

    dateTimeString = 2013-01-09 16:00:00
    myDate:        = 2013-01-09 16:00:00 +0000
    

    To present a date to the user, convert NSDate to NSString, using stringFromDate, but do not set a time zone (so that the local time zone is used):

    NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
    [dateFormat2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *localDateString = [dateFormat2 stringFromDate:myDate];
    NSLog(@"localDateString: %@", localDateString);
    

    Output:

    localDateString: 2013-01-09 17:00:00
    

    The time is now shown as 17:00:00, because my local time zone is “GMT+01”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sqlite3 database with a datetime field, the format of the date/time
I have this code in my viewWillAppear method: sqlite3 *database; if (sqlite3_open([[self dataFilePath] UTF8String],
I have written the Database class & trying to insert the values in the
I have a sqlite (v3) table with this column definition: timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
I have stored a variable in database(sqlite) with datetime data type. Now I want
I have a sqlite3 database in which I have corrupt data. I qualify corrupt
I have a Sinatra DataMapper app hitting a sqlite3 database that I am attempting
I have the following table in a SQLite3 database: CREATE TABLE overlap_results ( neighbors_of_annotation
I have some problem with reading from sqlite3 database. -(void) readMessengesFromDatabase { sqlite3 *database;
I have a grid of UIImageViews. The images names come from a sqlite3 database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.