How can remove time from Date string [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 8 years ago.
var x = new Date();
myVar = x.toString();
document.write(myVar);
// Sat Feb 14 2015 14:20:58 GMT+0100 (CET)
I want to remove the time <<14:20:58 GMT+0100 (CET)>>

Try ...
var x = new Date()
var myVar = x.toDateString();
This will only provide the date ...
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

Related

Comparing dateTimes in JavaScript is not working [duplicate]

This question already has answers here:
Greater than operator given wrong response inside console.log in javascript
(2 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I have 3 dateTimes in the same format. These 3 dateTimes have the same date at different times.
var orgstartdatetime = 'Mon Jan 01 1900 10:00:00 GMT+0519 (India Standard Time)';
var orgenddatetime = 'Mon Jan 01 1900 12:00:00 GMT+0519 (India Standard Time)';
var newdatetime = 'Mon Jan 01 1900 10:30:00 GMT+0519 (India Standard Time)';
I want to compare newdatetime is in between the orgstartdatetime and orgenddatetime.
I tried it like these 3 ways. But those are not working.
if (orgstartdatetime.getTime() <= newdatetime.getTime() < orgenddatetime.getTime() ) {
console.log('conflit');
}
if (orgstartdatetime.valueOf() <= newdatetime.valueOf() < orgenddatetime.valueOf() ) {
console.log('conflit');
}
if (+orgstartdatetime <= +newdatetime < +orgenddatetime ) {
console.log('conflit');
}
Please help me to fulfill this if condition.
Thank you
You first need to create a Date object to access getTime() method, try this:
var orgstartdatetime = new Date('Mon Jan 01 1900 10:00:00 GMT+0519 (India Standard Time)');

new Date returned invalid in safari but chrome? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
new Date('2020-08-18 07:52') is working in Chrome, it returned
Tue Aug 18 2020 07:52:00 GMT+0800 (Malaysia Time)
but safari gave me invalid date? what's the best way to fix this? this bug in safari is breaking my entire app.
without need for tools/plugins/packages, I would simply separate the received date:
var apiDate = '2020-08-18 07:52'
var [ date, time ] = apiDate.split(' ')
var [ year, month, day ] = date.split('-')
var [ hour, minute ] = time.split(':')
var newDate = new Date(year, month - 1, day, hour, minute, 0)
// Tue Aug 18 2020 07:52:00
I would just be careful about TimeZones, as your date has no time zone description ...
Note
from the code above, one might think that subtracting 1 from a string is invalid, but that's the caveats of javascript...
"08" + 1 = "081"
"08" - 1 = 7
if the format is always the same, you can also do:
var apiDate = '2020-08-18 07:52'
var newDate = new Date(`${apiDate.replace(' ', 'T')}:00`) // '2020-08-18T07:52:00'
// Tue Aug 18 2020 07:52:00

How do I compare two string Datetimes? [duplicate]

This question already has answers here:
Compare two dates with JavaScript
(43 answers)
Closed 2 years ago.
I have Datetimes formated as "16 Apr 2020 02:07 PM CST" and I need to compare it to another datetime just like that to know which date came first.
What I tried to do until now was:
var firstDate = "16 Apr 2020 02:07 PM CST";
var secondDate = "23 Apr 2020 06:07 AM CST";
var diff = Math.abs(firstDate - secondDate);
That or I tried to check if one was greater than the other and got similar results.
You should check time in milliseconds (timestamp) to compare dates:
var firstDate = new Date("16 Apr 2020 02:07 PM CST");
var secondDate = new Date("23 Apr 2020 06:07 AM CST");
if (firstDate.getTime() < secondDate.getTime()) {
console.log('First Date came first');
} else {
console.log('Second Date came first');
}

new Date() always read dd/mm/yyyy as mm/dd/yyyy(month first) hence day and month values get mixed resulting in NaN/Error [duplicate]

This question already has answers here:
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
When converting date for example:
var dateObj = new Date("10/01/2019");
console.log(dateObj);
returns Tue Oct 01 2019 00:00:00 i.e. it takes in the day as month and likewise with the month value
How to make new Date() to take dd/mm/yyyy ??
Answer is here: https://stackoverflow.com/a/33299764/6664779
(From original answer) We can use split function and then join up the parts to create a new date object:
var dateString = "23/10/2019"; // Oct 23
var dateParts = dateString.split("/");
// month is 0-based, that's why we need dataParts[1] - 1
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
document.body.innerHTML = dateObject.toString();

.js Date String YYYY-MM-DD-HH-MM [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
I am trying to get a string with date/time on this format: "YYYY-MM-DD-HH-MM"
I cannot find a proper way to do it?
This is what I have at the moment:
var x = new Date();
var TimeStamp = x.toString();
Outputs:
Tue Oct 30 2018 14:33:03 GMT+0000 (UTC)
Use Date methods and format it.
var date = new Date();
var output = date.getFullYear()+'-'+("0" + (date.getMonth() + 1)).slice(-2)+'-'+("0" + date.getDate()).slice(-2)+'-'+date.getHours()+'-'+date.getMinutes()
console.log(output)
Or you can use momentjs format.
console.log(moment().format('YYYY-MM-DD-HH-mm'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

Categories

Resources