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

  • SEARCH
  • Home
  • 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 8331743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:30:33+00:00 2026-06-09T02:30:33+00:00

I wrote code to extract the date from a given string. Given > Date:

  • 0

I wrote code to extract the date from a given string. Given

  > "Date: 2012-07-29, 12:59AM PDT"

it extracts

  > "2012-07-29" 

The problem is my code looks lengthy and cumbersome to read. I was wondering if was a more elegant way of doing this.

  raw_date = "Date: 2012-07-29, 12:59AM PDT"

  #extract the string from raw date
  index = regexpr("[0-9]{4}-[0-9]{2}-[0-9]{2}", raw_date) #returns 'start' and 'end' to be used in substring

  start = index #start represents the character position 's'. start+1 represents '='
  end = attr(index, "match.length")+start-1
  date = substr(raw_date,start,end); date
  • 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-09T02:30:35+00:00Added an answer on June 9, 2026 at 2:30 am

    You can use strptime() to parse time objects:

    R> strptime("Date: 2012-07-29, 11:59AM PDT", "Date: %Y-%m-%d, %I:%M%p", tz="PDT")
    [1] "2012-07-29 11:59:00 PDT"
    R> 
    

    Note that I shifted your input string as I am unsure that 12:59AM exists… Just to prove the point, shifted by three hours (expressed in seconds, the base units):

    R> strptime("Date: 2012-07-29, 11:59AM PDT", 
    +>          "Date: %Y-%m-%d, %I:%M%p", tz="PDT") + 60*60*3
    [1] "2012-07-29 14:59:00 PDT"
    R> 
    

    Oh, and if you just want the date, it is of course even simpler:

    R> as.Date(strptime("Date: 2012-07-29, 11:59AM PDT", "Date: %Y-%m-%d"))
    [1] "2012-07-29"
    R> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a code snippet to extract elementary information from an Atom feed (e.g.
I want to extract variables from math expression using c#. I wrote this code
I'm now learning how to extract exif from images.I wrote the simple code like
I wrote the following code to extract out the exceptions along with a string
I wrote some code to extract data from xls and save them into csv.
I'm trying to write some code to extract Exif information from a JPG. Exif
I wrote some code that activity starts a service to get some text from
I have this parameter below token[10] =Date: UTC 2012-02-02 17:09:04 i wrote the following
I wrote some codes in PHP to extract content of some elements from other
I'm using some code to extract from a Google Url the keyword of the

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.