When i use the new Date() instance i get something like
2020-12-10T12:30:18.108Z
the problem is that by me the hour on my local machine is 13:30 - but it gives me one hour earlier so 12:30 in the current time. How can i customize to give me the right time of my local machine ?
Also i don't know the meaning of the last four character after the . sign
108Z
all i need is to insert in my db the hour and the date without this signs
2020-12-10T12:30:18
is there any other solution except manupulating the string directly ?
You're using UTC Date I guess. You can try to use .toLocaleString() on your date time object.
Maybe use like that
var datetime = new Date();
var now = datetime.toLocaleString();
This should get you something like this: 10/12/2020, 8:47:15 AM
UPDATED
Another option if you want to maintain format and just remove T and Z characters is to replace string
You can try this:
var datetime = new Date();
var now = datetime.toISOString().replace('Z', '').replace('T', '');
Related
I'm trying to add the current time to an existing date but I'm not sure how to do it.
I'm importing stuff into a Postgres database and need a ISO string to update the "updatedAt" column, the imported stuff only has a date like this tho: "2022-03-15", no time.
How would I add the time to this and turn it into a proper ISO string for my database?
const date = new Date('2022-03-15')
const iso = date.toISOSTring() // how to add the current time?
-
Should look like this: "2022-03-15 09:36:54.292613"
Thank you! :)
Try to use dayJs and add the time that you need, https://day.js.org/docs/en/parse/string
dayjs('2018-04-04T16:00:00.000Z')
dayjs('2018-04-13 19:18:17.040+02:00')
dayjs('2018-04-13 19:18')
You can set the time units into date from the current date-time i.e. new Date().
const date = new Date("2022-03-15");
const now = new Date();
date.setHours(now.getHours());
date.setMinutes(now.getMinutes());
date.setSeconds(now.getSeconds());
date.setMilliseconds(now.getMilliseconds());
console.log(date.toISOString());
console.log(date.toISOString().replace("T", " ").replace("Z", " "));
I have a date selector and a time selector, and I'm trying to figure out how I can combine their outputs to make a single ISOString so that I can use with the google calendar API.
Here's what I've tried:
//date = 2022-05-18
//time = 14:22
const apptdate = new Date(date)
const timeSplit = time.split(':')
apptDate.setHours(timeSplit[0])
apptDate.setMinutes(timeSplit[1])
What I notice is when I console.log(apptdate) this is the output I get: 2022-05-17T18:22:00.000Z
I'm not sure why it changes the day from May 18 to May 17, and the time from 14:22 to 18:22.
Does anyone have a solution for this? Or even a completely different way of combining date and time to one string (other than using a datetime-local input format, I want to keep the date and time separate in my database).
"2022-05-18" is parsed as UTC but apptDate.setHours(timeSplit[0]) sets the local hour. So if the host has a negative offset, the local date is 17 May and the time is set to the local hour on 17 May, not UTC hour on 18 May.
Instead use setUTCHours and setUTCMinutes.
let date = '2022-05-18';
let time = '14:22';
let apptDate = new Date(date);
let timeSplit = time.split(':');
apptDate.setUTCHours(timeSplit[0]);
apptDate.setUTCMinutes(timeSplit[1]);
// 2022-05-18T14:22:00.000Z
console.log(apptDate.toISOString());
PS. There was also a typo: let apptdate then later apptDate.
I have this string date and I want to use it in a date type for querying in sequelize.
let date = "2019-08-08T12:53:56.811Z"
let startDate = new Date(date)
console.log(startDate)
->> 2019-08-07T12:53:56.811Z
when I am trying to insert to db.it changes with another hour.
let newTask = Task.create({ time_to_deliver: startDate})
console.log(newTask.time_to_deliver)
->> 2019-08-08 17:23:56
what is this? is it something about timezone and UTC time stuff?
I think if you will make the "startDate" key in your DB of 'Date' type instead of 'String' type it will work. I have checked this with MongoDB and it worked for me.
"startDate: {type: Date}"
according to #barbsan answer . sequelize changes the date to toLocalString() format. in querying return data it turns to the actual date.
you can simply use this javascript functions for better enhancement
var d = new Date("July 21, 1983 01:15:00");
var n = d.getDate(); //this will give you 21
there is also a similar function available - getMonth(),
First, get this date day and month and time [for the time you can use these functions => 1. getTime() and 2. new Date().toISOString()]
and then send these things separately and combine them whenever you want retrieve.
There is the also second approach;
First, convert your date and time into a timestamp and enter that timestamp into the database.
var myDate="26-02-2012";
myDate=myDate.split("-");
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(newDate).getTime());
I am trying to remove the Z from the end of a dateTime entity. The timezone of the API gets confused from the site I'm pushing the date from. Does anyone know a script that can remove the Z when a user types in a dateTime?
You're using UTC Date i'm guessing. You can try to use .toLocaleString() on your date time object.
Example
var datetime = new Date();
var now = datetime.toLocaleString();
This should get you something like this: 6/30/2017, 8:47:15 AM
Another option if you want to maintain the format and just remove the T and Z characters is to replace the strings. Example:
var datetime = new Date();
var now = datetime.toISOString().replace('Z', '').replace('T', '');
I want to get the time difference between saved time and current time in javascript or jquery. My saved time looks like Sun Oct 24 15:55:56 GMT+05:30 2010.
The date format code in java looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
How to compare it with the current time and get the difference?
Is there any inbuilt function in jquery or javascript??
Any suggestions or links would be appreciative!!!
Thanks in Advance!
Update
Date is stored as varchar in the DB. I am retriving it to a String variable and then change it to java.util.Date object. The java code looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
This date object was sent to client. There i want to compare the saved date with current date and want to show the time difference like 2 secs ago, 2 hours ago, 2 days ago etc... like exactly in facebook. I have gone through some date to timestamp conversion tutorial in java script and now i can get the difference in timestamp. Now, i want to know how i shall change it to some format like "2 secs or 2 days or 24 hours"??. Or, how i shall change it back to date format???
Convert them into timestamps which are actually integers and can get subtracted from each other. The you just have to convert back the resulting timestamp to a javascript date object.
var diff = new Date();
diff.setTime( time2.getTime()-time1.getTime() );
You dont need to explicit convert, just do this:
var timediff = new Date() - savedTime;
This will return the difference in milliseconds.
jQuery doesn't add anything for working with dates. I'd recommend using Datejs in the event that the standard JavaScript Date API isn't sufficient.
Perhaps you could clarify exactly what input and output you're aiming for. What do you mean by "the difference?" There is more than one way to express the difference between to instants in time (primarily units and output string formatting).
Edit: since you said you're working with jQuery, how about using CuteTime? (Demo page)