Sqlite time functions and time processing in iPhone applications

Source: Internet
Author: User
Tags julian day

IPhoneApplicationSqlite timeFunctions andTimeProcessing is the content to be introduced in this article.Sqlite timeFunctions andTimeThe processing method. Let's look at the details. This article is based onSQLiteIf there is anything wrong with the content translation in the official WIKI, I hope you can point out that, after all, my English skills are really poor.SQLiteIncludes the following fiveTimeFunction:

Date (date and time string, modifier, modifier ,......)

Time (date and time string, modifier, modifier ,......)

Datetime (date and time string, modifier, modifier ,......)

Julianday (date and time string, modifier, modifier ,......)

Strftime (date and time format, date and time string, modifier, modifier ,......)

The preceding five functions require a date and time string as the parameter, followed by zero to multiple modifier parameters. The strftime () function also requires a date and time format string as the first parameter.

The date () function returns a date in YYYY-MM-DD format;

The time () function returns a date time in the format of "YYYY-MM-DD HH: MM: SS;

The julianday () function returns the number of days, starting from January 1, November 24, 4714 BC, Greenwich Mean Time;

The strftime () function returns a formatted date and time, which can be formatted using the following symbol:

 
 
  1. % D day (December 31, January) 01-31
  2. % F seconds in decimal form, SS. SSSS
  3. % H hour 00-24
  4. % J day of the year 01-366
  5. % J Julian Day Numbers
  6. % M month 01-12
  7. % M minutes 00-59
  8. % S number of seconds calculated from January 1
  9. % S seconds 00-59
  10. % W weeks, 0-6, 0 is Sunday
  11. % W week of the year 00-53
  12. % Y year 0000-9999
  13. % Percent

The other four functions can be represented by the strftime () function:

 
 
  1. date(…)        ->  strftime(“%Y-%m-%d”,…)  
  2. time(…)        ->  strftime(“%H:%M:%S”,…)  
  3. datetime(…)   ->  strftime(“%Y-%m-%d %H:%M:%S”,…)  
  4. julianday(…)  ->  strftime(“%J”,…) 

Date and Time string, which can be in the following formats:

 
 
  1. YYYY-MM-DD  
  2. YYYY-MM-DD HH:MM  
  3. YYYY-MM-DD HH:MM:SS  
  4. YYYY-MM-DD HH:MM:SS.SSS  
  5. YYYY-MM-DDTHH:MM  
  6. YYYY-MM-DDTHH:MM:SS  
  7. YYYY-MM-DDTHH:MM:SS.SSS  
  8. HH:MM  
  9. HH:MM:SS  
  10. HH:MM:SS.SSS  
  11. now  
  12. DDDD.DDDD 

In the fifth to seventh formats, "T" is a character used to separate the date and time. The eighth to tenth formats only represent the Time of the 2000-01-01 day, 'Now 'in 11th formats indicates that the current date and time are returned, and the Greenwich Mean Time (UTC) is used; the other 12th formats indicate a Julian Day Numbers.

Modifier

You can use the following modifier to change the date or time:

 
 
  1. NNN days  
  2. NNN hours  
  3. NNN minutes  
  4. NNN.NNNN seconds  
  5. NNN months  
  6. NNN years  
  7. start of month  
  8. start of year  
  9. start of week  
  10. start of day  
  11. weekday N  
  12. unixepoch  
  13. localtime  
  14. utc 

The first six modifiers are the time and date of adding a specified value. The seventh to tenth modifiers indicate that the start of the current date is returned; the first modifier indicates that the next week is the date and time of N. The second modifier indicates the number of seconds since. The second modifier indicates that the local time is returned.

The following are some examples:

Current computer time

 
 
  1. SELECT date(‘now’) 

The last day of the current month of the computer

 
 
  1. SELECT date(‘now’,’start of month’,’+1 month’,’-1 day’) 

Calculate the date and time expressed by UNIX timestamp 1092941466

 
 
  1. SELECT datetime(‘1092941466’,’unixepoch’) 

Calculate the local Date and Time in UNIX timestamp 1092941466

 
 
  1. SELECT datetime(‘1092941466’,’unixepoch’,’localtime’) 

Current UNIX timestamp of the computer

 
 
  1. SELECT strftime(‘%s’,’now’) 

How many days do the two dates differ?

 
 
  1. SELECT jolianday(‘now’)-jolianday(‘1981-12-23’) 

How many seconds is the difference between two date and time?

 
 
  1. SELECT julianday('now')*86400 - julianday('2004-01-01 02:34:56')*86400 

Calculate the date of the first Tuesday of March.

 
 
  1. SELECT date('now','start of year','+9 months','weekday 2'); 

Get year

 
 
  1. strftime(‘%y’,'2008-4-28') 

Get month

 
 
  1. strftime(‘%m’,'2008-4-28') 

Similarly, we can use strftime to obtain other desired information, but remember to enclose the time with quotation marks.

 
 
  1. Example 1.
  2. Select datetime ('now ');
  3. Result: 12:55:54
  4.  
  5. Example 2.
  6. Select datetime ('2017-10-17 ');
  7. Result: 12:00:00
  8.  
  9. Example 3.
  10. Select datetime ('2017-10-17 00:20:00 ',' + 1 hour ','-12 minute ');
  11. Result: 01:08:00
  12.  
  13. Example 4.
  14. Select date ('2014-10-17 ',' + 1 Day', '+ 1 year ');
  15. Result:-10-18
  16.  
  17. Example 5.
  18. Select datetime ('now ', 'start of year ');
  19. Result: 00:00:00
  20.  
  21. Example 6.
  22. Select datetime ('now ', 'start of month ');
  23. Result: 00:00:00
  24.  
  25. Example 7.
  26. Select datetime ('now ', 'start of Day ');
  27. Result: 00:00:00
  28.  
  29. Example 8.
  30. Select datetime ('now ',' + 10 hour ', 'start of Day',' + 10 hour ');
  31. Result: 10:00:00
  32.  
  33. Example 9.
  34. Select datetime ('now ', 'localtime ');
  35. Result: 21:21:47
  36.  
  37. Example 10.
  38. Select datetime ('now ',' + 8 hour ');
  39. Result: 21:24:45

In Example 3, + 1 hour and-12 minute indicate that the first parameter of the datetime function can be added or removed for a certain period of time.

In Example 5, start of year indicates the start time of the year.

As shown in Example 8, although 2nd parameters are added for 10 hours, the time is reset to 00:00:00 by the 3rd parameter "start of day", and the subsequent 4th parameters are set at 00:00:00.
The time is increased by 10 hours to 10:00:00.

In Example 9, the Greenwich Mean Time Zone is converted to the local time zone.

In Example 10, the Greenwich Mean Time Zone is converted to UTC + 8.

The strftime () function converts a date string in the YYYY-MM-DD HH: MM: SS format to another form of string.

Strftime () syntax is strftime (format, date/time, modifier, modifier ,...)

It can be formatted with the following symbols:

 
 
  1. % D month, 01-31
  2. % F seconds in decimal form, SS. SSS
  3. % H hour, 00-23
  4. % J calculates the day of the year, 001-366
  5. % M month, 00-12
  6. % M minutes, 00-59
  7. % S seconds from January 1, January 1, 1970 to the present
  8. % S seconds, 00-59
  9. % W weeks, 0-6 (0 is Sunday)
  10. % W calculate the week of the year for a day, 01-53
  11. % Y, YYYY
  12. % Percent sign

The usage of strftime () is as follows:

 
 
  1. Example 11.
  2. Select strftime ('% Y. % m. % d % H: % M: % s', 'Now', 'localtime ');
  3. Result: 2006.10.17 21:41:09

In Example 11, the time is separated by dots and converted to the time in the local time zone.

Summary:IPhoneApplicationSqlite timeThe function and time processing content have been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.