How do I separate the date from the string - javascript

This is a string 2011-11-09 00:00:00
So now how do I separate the date i.e. 2011-11-09 from the string, I dont want to use the slicing here if anyone has better options or ideas please let me know..

var date = '2011-11-09 00:00:00'.split(' ')[0];

You can split it by ' ' and then the first element will contain what you want.
console.log('2011-11-09 00:00:00'.split(' ')[0]);

Like this:
var date = 'This is a string 2011-11-09 00:00:00'.match(/\d{4}-\d{2}-\d{2}/)

if you dont want to use splicing, like you posted you could create a Date obj
var newDate = new Date(2011-11-09 00:00:00);
then to get the date, just use the toString override
var dateOnly = newDate.toString("YYYY-mm-dd");
That is if you dont want to use splicing or splits

Related

How can get only date without time from a string contain date and time?

I've a date like that 01/03/22 22:09 and I want to store it in DB just in a format of date like like that '2022-03-01'; I've tried Regex but still get time in an hour:minute too
How can I delete it?
const date_time = '22/01/03 22:09';
const only_date = date_time.replace(/(\d{2})\/(\d{2})\/(\d{2})/,"'20$3-$2-$1'");
//console.log(only_date.substring(0,only_date.lastIndexOf("'")+1))
console.log(only_date)
PS: without using substring or slice... just with regex is there a way to do that?
You missed the .*
const date_time = '22/01/03 22:09';
const only_date = date_time.replace(/(\d{2})\/(\d{2})\/(\d{2}).*/,"'20$3-$2-$1'");
console.log(only_date)

Using split pop and join all at once can it work?

I really love Javascript and I wrote my code like this. I feel like it should work. Am I doing it in the wrong order? If it won't work like this why not?
var mydate = new Date();
alert( mydate.toLocaleTimeString().split(":").pop().join(':'));
split() makes it an array, pop() takes off the end of the array, join() makes it a string again right?
You could use Array#slice with a negative end/second argument.
Array#pop returns the last element, but not the array itself. slice returns a copy of the array with all emements from start without the last element.
var mydate = new Date();
console.log(mydate.toLocaleTimeString().split(":").slice(0, -1).join(':'));
No, pop() will remove the last element from the array and return it.
To achieve what you're trying, you'll need to first assign the result of split() to a variable you can then reference:
var mydate = new Date(),
myarr = mydate.toLocaleTimeString().split(':');
myarr.pop();
console.log(myarr.join(':'));
if all you want to achieve is hours:minutes, you can just simply do this
var mydate = new Date();
console.log(mydate.getHours() + ':' + mydate.getMinutes());
You are trying to use method chaining where next method in the chain uses the output of the previously executed method. Reason it's not working is because "join()" method is a prototype of an array but "pop()" returns an array element which doesn't aforementioned method that's why the error.
refactor your code as below:
var myDate = new Date(),
myDateArr = myDate.toLocaleTimeString().split(':');
myDateArr.pop(); // Remove the seconds
myDate = myDateArr.join(':'); // Returns string
console.log(myDate);
Hope this helps.
Try this
var mydate = new Date();
alert( mydate.toLocaleTimeString().split(":").slice(0, 2).join(":"));

moment.js compare dates via .diff()

Im having problems when I try to compare 2 dates.
var end = moment(items[i].dateEnd).format('DD/MM/YYYY');
var now = moment().format('DD/MM/YYYY');
Example: now = '29/10/2015' and end = '30/06/2015'
Tried using .diff() function from moment.js without any result, like this:
end.diff(now);
Any help?
Thanks.
You can try with specifying input date format:
moment(items[i].dateEnd, 'DD/MM/YYYY').diff(moment());

Get time difference between two date strings

I want to develop a JavaScript function to calculate the activity of users based on the date in the server where the data is stored. The problem is that the date is a string like this:
2013-08-11T20:17:08.468Z
How can I compare two string like this to calculate minor and major time as in the example?
If you want to compare two dates just use this :
var dateA = '2013-08-11T20:17:08.468Z';
var parsedDateA = new Date(dateA).getTime();
var dateB = '2013-06-06T17:33:08.468Z';
var parsedDateB = new Date(dateB).getTime();
if(parsedDateA > parsedDateB) {
// do something
}
Assuming you need to do the comparisons client-side, the best way is to load the dates into Date objects using Date.parse. Then compare them using the functions provided for Date, such as getTime.
Try parse method:
var s = "2013-08-11T20:17:08.468Z";
var d = Date.parse(s);
As I have understood you in the right way, there is a good answer to your question here.
You can also look at this very good Library (DateJS).
If your problem was converting from the Date-String to js-Date look at this Page.

manipulating date object with javascript

i have been tinkering with the date object.
I want to add a dynamic amount of days to a day and then get the resulting date as a variable and post it to a form.
var startDate = $('#StartDate').datepicker("getDate");
var change = $('#numnights').val();
alert(change);
var endDate = new Date(startDate.getFullYear(), startDate.getMonth(),startDate.getDate() + change);
does everything correctly except the last part. it doesnt add the days onto the day
take this scenario:
startdate = 2011-03-01
change = 1
alert change = 1
endDate = 2011-03-11 *it should be 2011-03-02*
thank you to all the quick replies.
converting change variable to an integer did the trick. thank you.
parseInt(change)
just to extend on this: is there a way to assign a variable a type, such as var charge(int)?
You may have fallen victim to string concatenation.
Try changing your last parameter in the Date constructor to: startDate.getDate() + parseInt(change)
See this example for future reference.
convert change to a number before adding it. it looks like you're getting a string concatenation operation rather than the addition you're expectingin your code.
I believe you are concatenating instead of using the mathematical operator. Try this instead,
var endDate = new Date(startDate.getFullYear(), startDate.getMonth(),startDate.getDate() + (+change));
It looks like you are not adding the ending day, you are concatinating it so '1' + '1' = '11'
use parseInt() to make sure you are working with integers
example
var change = parseInt($('selector').val());
Also, with this solution, you could easily end up with a day out of range if you are say on a start date of the 29th of the month and get a change of 5

Categories