12

I have a date\time string:

Fri Feb 08 2013 09:47:57 GMT +0530 (IST)

I need to extract the date (02/08/2013) and time (09:47 am) parts and store them in two variables.

Is there an efficient way to do it using JavaScript?

I have written the following code:

var day = elementDate.getDate(); //Date of the month: 2 in our example
            var monthNo = elementDate.getMonth(); //Month of the Year: 0-based index, so 1 in our example
            var monthDesc = {'0':'January', '1':'February'}; //, "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            var year = elementDate.getFullYear() //Year: 2013
            var hours = elementDate.getHours();
            var mins = elementDate.getMinutes();
            var lDateValue = (year.toString() + "-" + monthNo.toString() + "-" + day.toString());

document.getElementById("lDate").value = lDateValue;

I have this in my HTML:

<input type="date" name="name" id="lDate" class="custom" value=""/>
                <input type="time" name="name" id="lTime" class="custom" value=""  />

The fields are not getting updated. Am I missing something?

1
  • The date constructor should be able to take that string otherwise you can use regular expressions. Commented Feb 9, 2013 at 10:44

3 Answers 3

32

The Date constructor is very good at creating dates from strings:

Use the following:

// This could be any Date String
var str = "Fri Feb 08 2013 09:47:57 GMT +0530 (IST)";
var date = new Date(str);

This will then give you access to all the Date functions (MDN)

For example:

var day = date.getDate(); //Date of the month: 2 in our example
var month = date.getMonth(); //Month of the Year: 0-based index, so 1 in our example
var year = date.getFullYear() //Year: 2013
Sign up to request clarification or add additional context in comments.

Comments

16

You can directly get the date and time string using Date constructor with the help of methods .toLocaleDateString() and .toTimeString() as follow-

let dateStr =new Date("Wed Aug 05 18:11:48 UTC 2020")

dateStr.toLocaleDateString()
// "05/08/2020"

dateStr.toTimeString()
// "23:41:48 GMT+0530 (India Standard Time)"

Comments

1

Convert to an ISO String and Split by "T".

Not sure what you're doing with the Date and the Time separately. But, if you have any intention of storing them in a database, it's best to store them in UTC time zone and in ISO format (e.g. 2013-02-08 and 09:47:57).

To get the date and time in this format, you can do something like:

const dateTimeInParts = yourDateTime.toISOString().split( "T" ); // DateTime object => "2021-08-31T15:15:41.886Z" => [ "2021-08-31", "15:15:41.886Z" ]

const date = dateTimeInParts[ 0 ]; // "2021-08-31"
const time = dateTimeInParts[ 1 ]; // "15:15:41.886Z"

I think the date in this format is good to go, but you may also want to strip the milliseconds and "Z" from the time, depending on where and how you're going to use it.

3 Comments

You lose timezone info if done this way. If you want to display this to user in their local timezone, this isn't ideal. Have to take the longer way of using .getDate(), .getMonth(), etc.
Do not use this! As @xyres said, you'll lose time zone info. E.g. if your date was 2024-01-01 00:00 GMT + 1, the method toISOString().split( "T" ) will return 2023-12-31 instead of the intended 2024-01-01.
Hmm, well if the Timezone is GMT +1 then the actual date is 2023-12-31. In my answer I say to convert them to UTC time (i.e. GMT or Z) first. Then you can split out the date and time components and know that this is in UTC. So, again, it really depends on what you're doing. In my case, I literally just needed to split the date and time portions of an ISO String format, which this works for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.