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
Luiscri
  • 913
  • 1
  • 13
  • 40

3 Answers3

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.

RK_15
  • 929
  • 5
  • 11
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
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 30 '19 at 20:35
  • @RobG I am sorry, but why are you asking about ‘Date.parse()’? In my view, I did not used ‘Date.parse()’ function. – StepUp May 01 '19 at 14:44
  • `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 May 01 '19 at 20:23
  • @RobG Thanks for your great comment! It was really helpful! I've edited my answer. – StepUp May 03 '19 at 04:07
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>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62