I am trying to store date in DB using the current timezone.
I have used library date-fns-tz When I convert the date as a string is okay, but when I want to create that string as a Date it is still a problem.
const date = new Date();
const date_string = formatInTimeZone(
date,
"Europe/Rome",
"yyyy-MM-dd HH:mm:ss"
);
console.log(date_string); //2022-04-16 11:46:28 (Right time)
const current_date = new Date(date_string); // 2022-04-16T09:46:28.000Z (Converted to 2h back)
Related
I am trying to convert date object to ISOString() formate. But it return me 1 day off ( I mean it reduce 1 day ).
var fromDate = {
day:4,
month:5,
year:2012
}
var fromDateString = new Date(fromDate.year+'-'+fromDate.month+'-'+fromDate.day)
console.log(fromDateString.toISOString())
It is because of timezone, new Date() is in your current timezone, toISOString() is using standard timezone.
I have searched and I find best solution for every date object to convert in any format.
Comment if you agree?
var dateObj = {
day:2,
month:5,
year:2012
}
var date = new Date;
date.setFullYear(dateObj.year,dateObj.month-1,dateObj.day)
console.log(date)
I have a date in (yyyy-mm-dd) format, i want to get start and end date with timestamps and ISO format, i can concat date and timestamp but i want it in date type not in string,
let date = "2020-09-11";
Expected result should be,
startDate = 2020-09-11T00:00:00.000Z
endDate = 2020-09-11T23:59:59.000Z
The start date is correct, The end date is not correct, it gives different time stamp, 18:29:59.000Z when i set it to setHours(23,59,59),
let startDate = new Date(date);
console.log(startDate);
// output: 2020-09-11T00:00:00.000Z
let endDate = new Date(new Date(date).setHours(23,59,59));
console.log(endDate);
// output: 2020-09-11T18:29:59.000Z
I looked at this question convert string to ISO date time in nodejs, but this does not help me, i don't want to use moment,
Am i doing wrong to convert dates? i am using nodejs.
You should use setUTCHours()
The setUTCHours() method sets the hour for a specified date according to universal time
let date = new Date()
let startDate = new Date(new Date(date).setUTCHours(0,0,0));
let endDate = new Date(new Date(date).setUTCHours(23,59,59));
console.log(startDate);
console.log(endDate);
You could just add the postfix without conversion to a local date.
let date = "2020-09-11",
startDate = date + 'T00:00:00.000Z',
endDate = date + 'T23:59:59.000Z';
console.log(startDate);
console.log(endDate);
I have a date time picker and it omits date in format of a DD-MM-YYYY HH:mm a format. I want to convert it into epoch time to store it in the database backend.
I have tried using the following methods so far, but all of them return NaN as output:
var loadingPlannedUnTime = this.state.loadingPlannedUnTime;
console.log("normal conversion"+loadingPlannedUnTime);
//returns 9-08-2020 15:08:00
//Moment method
var loadingPlannedUnTime = moment(this.state.loadingPlannedUnTime).unix();
console.log("moemnt"+loadingPlannedUnTime)
//Returns NaN
//JS getTime() method
var pickupDateTime = new Date(this.state.loadingPickupTime);
var loadingPickupTime = pickupDateTime.getTime();
console.log("new date and gettime"+loadingPickupTime)
//Returns NaN
What is the correct method to convert it to epoch time?
https://momentjs.com/docs/
If you know the format of an input string, you can use that to parse a moment.
moment("12-25-1995", "MM-DD-YYYY");
const x = moment('24-12-2019 09:15', "DD-MM-YYYY hh:mm");
console.log(x.format())
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
const matches = loadingPlannedUnTime.match(/(\d{1,2})-(\d{1,2})-(\d{2,4}) (\d{1,2}):(\d{1,2}):(\d{1,2})/);
if (!!matches) {
// new Date(year, month, day, hours, minutes, seconds, milliseconds)
const epoch = new Date(matches[3], matches[2] - 1, matches[1], matches[4], matches[5], matches[6]).getTime();
console.log(epoch);
}
I am trying to convert string to time, the string i have are in this format, '8:3' and '16:45'.
I want to convert UTC time in jQuery.
You can write your function to create UTC date with the time string.
function toUTC(str) {
let [h, m] = str.split(':');
let date = new Date();
date.setHours(h, m, 0)
return date.toUTCString();
}
console.log(toUTC('8:3'))
console.log(toUTC('16:45'))
You don't need jQuery for such operations. Just the simple Date object will do the trick. Say you want to convert time from a specific date.
let date = new Date('2020-04-01'); // leave the Date parameter blank if today
date.setHours(16); // must be 24 hours format
date.setMinutes(45);
let theUTCFormat = date.getUTCDate();
Cheers,
Can anyone help me to convert date format 27.05.2015 01:46:32.UTC to Locale time.
I tried the following code
var date = new Date('27.05.2015 01:46:32.UTC ');
date.toString();
This is also not working.
Even momentjs is also gives error to convert this format of date.
As per statement even momentjs is also gives error to convert this format of date, I have taken liberty and used momentjs here.
You need to use string-format moment constructor to pass string and format in which input string is.
Use
var t = "27.05.2015 01:46:32.UTC";
//pass dateTime string and its format, it will return
//moment object
var cdt = moment.utc(t, 'DD.MM.YYYY HH:mm:ss');
//Get date object
var date = cdt.toDate();
//Use date variable as per requiremnt
alert(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
The format you are using for Date is DD.MM.YYYY. So you have to change this to MM.DD.YYYY for a valid java script object.
var date = '27.05.2015 01:46:32.UTC';
var date1 = date.split(' ');
console.log(date1)
var date2 = date1[0].split('.');
console.log(date2)
var date3 = date2[1]+ '.' +date2[0] +'.'+ date2[2];
console.log(date3)
var final_date = date3 + ' ' + date1[1];
console.log(final_date);
final_date.toString();