So I am a little bit confused. the server return date is in UTC ISO format, my local time is CST.
so I use toLocaleString() and then subtract the difference but I get a negative number. I rather use react native library rather than installing new libraries such as moment
export function timeSince(date) {
//date is 2022-11-07T18:36:39.543 which is UTC, not CST
date = date.toLocaleString("en-US", {
timeZone: "CST",
});
// somehow stayed 2022-11-07T18:36:39.543
date = new Date(date);
//Mon Nov 07 2022 18:36:39 GMT-0600 (Central Standard Time)
Mon Nov 07 2022 12:29:21 GMT-0600 (Central Standard Time)
'2022-11-07T18:27:38.03'
var seconds = Math.floor((new Date() - date) / 1000); // -21506
return seconds
}
This question already has answers here:
Add one day to date in javascript
(11 answers)
Closed last year.
I want to add one day or 12 hours to a date in the format of
Thu Mar 03 2022 12:00:00 GMT
I have tried:
new Date(value.startDate + 1);
but it does nothing.
Please help me out, I am new to JavaScript.
If you want to add something to your timestamp, this will do the trick no matter what you want to add.
const timestamp = new Date("Thu Mar 03 2022 12:00:00 GMT")
console.log(timestamp.toString())
// because getTime and setTime uses milliseconds:
const millisecondsToBeAdded = 12 * 60 * 60 * 1000
timestamp.setTime(timestamp.getTime() + millisecondsToBeAdded)
console.log(timestamp.toString())
Try this
const date = new Date("Thu Mar 03 2022 12:00:00 GMT");
date.setDate(date.getDate() + 1);
I have user inputs of meeting_time which is javascript Date (calendar select) and meeting_time string (select dropdown option)
var userTime = meeting_time ? meeting_time.label.split(' -')[0] : null
var userDate = moment(meeting_date).format("YYYY-MM-DD");
How Do I converting those variable to moment.js object with specific timezone
For example,
meeting_date is Thu Jul 12 2018 12:00:00 GMT+0300 (MSK)
meeting_time is 15:00 - 15:30
I want to get moment _d Thu Jul 12 2018 20:00:00 GMT+0800 (UTC +3 to UTC +8)
You can set the hour and minute in the date.
const [hour, minute] = userTime.split(':');
moment(meeting_date).hour(hour).minute(minute).toDate();
I have this string "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" and I need to convert it to the timezone (GMT-0300) using just moment.js (not moment-timezone.js)
I'm doing this but it's returning the same hour.
var startDateTime = moment(date).format('YYYY-MM-DD HH:mm Z'),
startMoment = moment.parseZone(startDateTime).zone();
console.log(moment(startDateTime).zone(startMoment).format("YYYY-MM-DD HH:mm"));
Any help ?
To convert a moment object with timezone A to timezone B, you can do the following:
var startDateTime = "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)";
var newDateTime = moment(startDateTime).zone('-0400').format('YYYY-MM-DD HH:mm')
Also note that in your example, the initial time "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" is already GMT-0300.
I get time in milliseconds from the server. I convert it to Date and get -
Mon Jul 22 2013 11:16:01 GMT+0200 (W. Europe Daylight Time) as the date in the record.
I want to separate out data of Monday, Tuesday etc into arrays. I am thinking of converting this date to Mon Jul 22 2013 23:59:59 GMT+0200 (W. Europe Daylight Time) and then filter out the records.
How can i change the date to the required end of the day time? or is there an easier way to do this ?
You could always construct a new DateTime object just using the year, month and day properties from the existing date, like so:
var actualDate = new Date(); // 2013-07-30 17:11:00
var endOfDayDate = new Date(actualDate.getFullYear()
,actualDate.getMonth()
,actualDate.getDate()
,23,59,59); // 2013-07-30 23:59:59
For future visitors, just use
var start = new Date();
var end = new Date();
start.setHours(0,0,0,0);
end.setHours(23,59,59,999);
Using http://momentjs.com:
var now = new Date().getTime();
var endOfDay = moment(now).endOf("day").toDate(); // Wed Jan 20 2016 23:59:59 GMT-0800 (PST)
var actualDate = new Date()
var eodDate = new Date(Math.floor(actualDate.getTime()/86400000+1)*86400000 + actualDate .getTimezoneOffset()*60000 - 1000)
where 86400000 are total milliseconds in a day
If two Date Objects are on the same day then they have the same Date String:
new Date('1374488161000').toDateString()
=> "Tue Jul 30 2013"
new Date('13744917610403').toDateString()
=> "Tue Jul 30 2013"
Although a rather naive method of comparing days, it's probably the simplest comparison.