0

I'm trying to get a Date js object from a string with a specific format.

My string date looks like this:

2019-04-22 05:00:11

Im trying to do it this way, but I don't achieve the expected results:

date_string = "2019-04-22 05:00:11"
date = Date.parseDate(date_string, "YYYY-MM-DD HH:mm:ss")

After this I want to add this Date object a time_delta of 30 minutes and represent it on string format, which I think it should be done this way:

new_date = new Date(date.getTime() + i*30*60000)
final_date_string = new_date.toString();

I want final_date_string to look like this:

2019-04-22 05:30:11

3 Answers 3

2

Try this code:

let [y,M,d,h,m,s] = '2019-04-22 05:00:11'.split(/[- :]/);
new Date(y,parseInt(M)-1,d,h,parseInt(m)+30,s);

Split date string based on the separators and then convert it to date object with whatever time change you need.

Sign up to request clarification or add additional context in comments.

Comments

1

Thanks for the great comment to @RobG for the potential error of Date.parse() method.

Try to use getTime() function of Vanilla JavaScript and add 30 seconds:

let date_string = "2019-04-22 05:00:11"
let dateArray = date_string.split(/[- :]/);
let plusThirty = new Date(dateArray[0], parseInt(dateArray[1]-1), parseInt(dateArray[2])
    , dateArray[3], parseInt(dateArray[4]) + 30, dateArray[5]);    
console.log('plusThirty: ', plusThirty);

let dateString = plusThirty.getFullYear() + "-" + (plusThirty.getMonth() + 1) 
    + "-" + plusThirty.getDate() + " " + plusThirty.getHours() + ":" 
    + plusThirty.getMinutes() + ":" + plusThirty.getSeconds();
console.log('dateString: ', dateString);

Output:

plusThirty:  Mon Apr 22 2019 05:30:11
dateString:  2019-4-22 5:30:11

4 Comments

@RobG I am sorry, but why are you asking about ‘Date.parse()’? In my view, I did not used ‘Date.parse()’ function.
new Date(date_string) uses the built–in parser. You should not use the built–in parser for any non–standard format, e.g. in Safari new Date("2019-04-22 05:00:11") returns an invalid date. You really shouldn't even use if for standardised formats as you will still get unexpected results for some formats.
@RobG Thanks for your great comment! It was really helpful! I've edited my answer.
0

You can use momentjs for your requirement, momentjs is popular library for DateTime handle

let date_string = "2019-04-22 05:00:11"
let date = moment(date_string, "YYYY-MM-DD HH:mm:ss");
date.add(30, 'minutes');

console.log(date.format('YYYY-MM-DD HH:mm:ss'));

let date_string = "2019-04-22 05:00:11"
let date = moment(date_string, "YYYY-MM-DD HH:mm:ss");
date.add(30, 'minutes');

final_date_string = date.format('YYYY-MM-DD HH:mm:ss')
console.log(final_date_string);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>

2 Comments

Thanks, that was exactly what I wanted
There is no need for either jQuery (which is irrelevant and not used anywhere in the answer) or moment.js for such a simple task.

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.