I need to store an unix value from a Time input.
The problem is that :
// I create a Moment Date from my input :
var date = moment({hour: 10, minute: 00)
// gives this _d : Mon May 04 2015 10:00:00 GMT+0200 (CEST)
// I convert it to unix value
date = date.unix()
// -> 1430726400
moment( date ).format('HH:mm')
// -> "14:25" // Should give me 10:00
// Online conversion unix time gives me : Mon, 04 May 2015 08:00:00 GMT
So how can I keep my 10:00 in memory as unix value using those transformations ?
Per documentation:
moment.unix( date ).format('HH:mm')
I kind of sorted it this way :
// I create a Moment Date from my input :
var date = moment({hour: 10, minute: 00)
// gives this _d : Mon May 04 2015 10:00:00 GMT+0200 (CEST)
// Convert to unix
date = date.toDate();
date = date.getTime();
console.log(moment( date ).format('HH:mm')); // Gives me 10:00
I hope it will keep the good time ? In France the time changes twice a year ( go forward 1 hour, go backwards 1 hour)
Related
my default timezone is (UTC + 8:00) Kuala Lumpur, Singapore and the computations is working fine but when I change my pc timezone to (UTC-6:00) Central Time (US & Canada) the result of is off by 1 day.
In (UTC + 8:00) Kuala Lumpur, Singapore Time the output from this.referenceMilestones[key]) is 2022-10-20 and after using the getRefMileStoneDate function the result is
#result which is correct
refMilestoneDate-- Thu Oct 20 2022 00:00:00 GMT+0800 (Philippine Standard Time)
But in timezone (UTC-6:00) Central Time (US & Canada) , the result if off by one day
#result which is wrong , it is now 19 which is supposed to be 20
refMilestoneDate-- Wed Oct 19 2022 00:00:00 GMT-0400 (Eastern Daylight Time)
Any idea how we can solve this one that the date should be consistent regarding of the timezone ? Thanks for helps and ideas.
#Code
const refMilestoneDate = this.getRefMileStoneDate(calculateFields[i].referenceMilestoneName);
console.log('refMilestoneDate--', refMilestoneDate)
getRefMileStoneDate(key:string):Date{
if (this.referenceMilestones && this.referenceMilestones[key]){
console.log(' ' , this.referenceMilestones[key])
return new Date(this.convertDateStringToYYYYMMDD(this.referenceMilestones[key]));
}
return null;
}
convertDateStringToYYYYMMDD(dateString: any) {
if (dateString) {
const dateObject = new Date(dateString);
return dateObject.toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '-');
}
return '';
}
JavaScript, especially at the client side, typically assumes you're working in local times. You probably want to work exclusively in UTC via the various UTC methods available in the Date object.
Otherwise, you'll want to look into use a date library such as Luxon which will allow you precise control of time zones both when parsing timestamps and when formatting them for display.
I have a date coming from database which is 2022-10-31 11:33:07.861Z, and in my js file I have this date is saved in a variable :
function test() {
let now = new Date().toLocaleString('en-GB', { timeZone: 'Europe/London'}); // I need now to be Europe/london time
let dateFromDb = db_date; // 2022-10-31 11:33:07.861Z
// I have a function which takes ^ above date and returns this : Mon Oct 31 2022 11:33:07 GMT+0000 (Greenwich Mean Time)
let formatedDate= this.toDate(dateFomDb); // returns : Mon Oct 31 2022 11:33:07 GMT+0000 (Greenwich Mean Time)
}
When I console.log(now, typeOf now) I get this: 02/11/2022, 23:24:52 string
and console for formatedDate returns this : Mon Oct 31 2022 11:33:07 GMT+0000 Object
I need to compare both dates :
return newDate <= now; // expect it to true or false;
Can anyone please suggest me how can I do this ?
I tried to cast both dates toLocalString but its string from I need object.
Hello i am trying to add time zone offset for start & end date.
In order to get something like this
start date: Mon May 30 2022 05:00:00 GMT+0000 here offset is (00:00 + 5 hours US central time offset)
end time: Wed Jun 01 2022 04:59:00 GMT+0000 (23:59 + 5 hours offset)
But getting for start date Mon May 29 2022 00:00:00 GMT+0000
Here is my code
export function convertIsoToMomentLocal(iso: string | ParsableDate, timezone = 'CST') {
if (iso && timezone) {
const mo = moment.tz(iso, timezone).startOf('day')
const offset = mo.utcOffset()
const epoch = mo.add(offset, 'm').unix()
console.log(epoch, 'test')
return epoch
}
}
So basically i am trying to determine start date and end date first, and only after that add time zone offset.
Any ideas tnx, i have tried everything.
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 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.