I'm using Google Apps Script making an API request to get a date. The date, however, is coming back as a number in scientific notation (1.635218706E9) which I'm assuming is in the format of milliseconds from 1970. After converting it from scientific notation to a regular number (1635218706) creating a new javascript date with it the wrong date and year. It should be Oct 25 2021, but instead gives Dec 31 1969.
Am I missing something when creating a new date from the number? And is there a way just have the API respond with a more readable date instead of milliseconds from 1970?
Just in case. I found if you multiply this number by 1000 and feed it to Data object you can get something a little bit more reasonable:
var seconds = 1.635218706E9;
var date = new Date(seconds*1000);
console.log(date); // output: 2021-10-26T03:25:06.000Z
var date_str = [date.getFullYear(), date.getMonth()+1, date.getDate()].join('/')
console.log(date_str); // output: 2021/10/26
It looks like the API gives you seconds.
you can use toLocaleString / toLocaleDateString
so the code is like this
const dates = new Date()
const fullDates = dates.toLocaleString('en-US')
you can read in documentation here :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
Related
I'd like to get the Epoch timestamp of a date being entered by a user, but convert the time to 12:01 am in JavaScript.
How do I do that?
Take the timestamp and pass it while creating a instance of the Date class like:
let timestamp = 1629289414;
let dateInstance = new Date(timestamp * 1000);
console.log(dateInstance); // Wed Aug 18 2021 14:23:34....
From there on you have many ways to work with dateInstance. To get the 12:00am result from this it's more a string manipulation/adjusting thing.
Just check out the documentation on the the javascript Date instance: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date
If you have further questions just post the code you've written and maybe I can help you out
I have to convert local date and time to utc format.
Therefore if I have date as 2021-08-11 (YYYY-MM-DD) and time as 2:40 PM, then slot date time should be 2021-08-11T09:10:00.000Z.
I have tried multiple things, but failed
const dateTimeInUTC = moment(
`${formattedDate} ${formatTime}`,
'YYYY-MM-DD HH:mm:ss'
).toISOString();
above code resulted me => 2021-08-10T21:10:00.000Z (which is +5:30 more)
Also, tried following
const formatted = formattedDate + " " +formatTime (2021-08-11 02:40 PM)
const result = new Date(formatted).toISOString();
this gave me
Range error :Invalid Date
However, this works as expected in console, but gives error in mobile.
I tested it now, convert - to / in date format then it will work fine in react native both on browser console and mobile. for more info you can check that link
var isoDateString = new Date('2021/08/11 12:00 pm').toISOString();
console.log(isoDateString);
If you want to use your date format (date witn -) then you to add T instead of space and the time should be on 24 hour scale
like
var isoDateString = new Date('2021-08-11T13:00').toISOString();
this solution will also work for you.
thanks
you can try that
var isoDateString = new Date('2021-08-11 2:40').toISOString();
console.log(isoDateString);
If you want to covert current System Time to UTC then Do :
const dateTimeInUTC = new Date().toUTCString();
console.log(dateTimeInUTC);
Or if you want convert any specific Date-Time to UTC then Do :
const dateTimeInUTC = new Date("October 13, 2000 00:45:00").toUTCString();
console.log(dateTimeInUTC);
I want to save data to database in order of their upload, I decided to use current time in ISO, like if date is -> 29 June 2021 Tuesday, 19(hour):36(minute):45(seconds), I extracted time from it as 193645 this ensured to display data according to time for that day,
but if the seconds is single digit for example(14(hour):32(minute):05(seconds)) but then it displays 14325 without 0.
And if possible I want the time for this date - (29 June 2021 Tuesday, 19(hour):36(minute):45(seconds))
to (without those brackets) - 21(for year)06(for month)29(for day)19(for hour)36(for minute)45(for seconds)
let _today = new Date().toISOString()
let dateToday = new Date(_today)
_time = dateToday.getHours().toString() + dateToday.getMinutes().toString() + dateToday.getSeconds().toString()
console.log(_time)
This is my current code
You can remove the separator symbols and take the substring you need.
Like in this function
symbolless = (date) => date.toISOString().replace(/[\-\.\:ZT]/g,"").substr(2,10)
Handling timezones is a little more difficult as JavaScript uses UTC-0 as the timezone for .toISOString().
Get the timezone in negative minutes using .getTimezoneOffset() and multiply with -60000 to get the correct offset in milliseconds. Be aware the timezone will depend on the machines local timezone, so deployments or users in other timezones will yield different results when running the command (this might be fixed in the future with the introduction of temporal).
symbolless = (date) => new Date(date.getTime() + date.getTimezoneOffset() * -60000)
.toISOString()
.replace(/[\-\.\:ZT]/g,"")
.substr(2,10)
Running the new function symbolless(new Date('August 19, 1975 23:15:30 UTC')) will yield 7508200015 in my timezone. In the following format:
YYMMDDHHMM
7508200015
This is correct as the date is now in UTC+1.
For example, I have this string "2020-09-09T21:00:14.114-04:00"
I grab this from my database and in its current form, it is a string. my goal is to have it display
4 PM instead of the long string of jibberish
is it possible to accomplish this?
I was thinking of possibly creating a new date object like
let test = new Date('2020-09-09T21:00:14.114-04:00').
but I'm stuck at the parsing and formatting part. it would be better to have this be done while the current state is a string but I don't think that this would be possible
edit: i would like the desired output to be the hour:minute and then am/pm
ex 10:15pm
You can do that by parsing the date from your database using Date.parse().
Then you can get the time or whatever you need using date.toLocalTimeString() in your case.
let dateUnix = Date.parse('2020-09-09T21:00:14.114-04:00');
const time = new Date(dateUnix).toLocaleTimeString();
console.log(time); // --> "4:00:14 AM"
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
Here's some useful resources MDN Date.parse()
MDN Date.toLocalTimeString()
You can do as following way.new Date() is used to get the current date and time.
var today = new Date();
var time = today.getHours();
if(time>12){
var new_time= time % 12;
}
else{
var new_time= time;
}
How can I get the timestamp after manipulating days in Moment.js?
I tried use:
var a = moment().subtract(10, 'days').calendar().getTime()
to get the timestamp, but failed.
It is not really clear what you mean by "timestamp".
To get the number of milliseconds since the Unix epoch, use moment().valueOf();. This corresponds to JS Date.getTime();.
To get the number of seconds since the Unix epoch, use moment().unix();.
To get the hour/minute/second as numbers, use moment().hour(); / moment().minute(); / moment().second();.
To get the ISO 8601 string (recommended for sending data over the wire), use moment().toISOString(); (e.g. "2013-02-04T22:44:30.652Z").
To print a user readable time, use moment().format();, e.g. moment().format('LTS') will return the localized time including seconds ("8:30:25 PM" for en-Us).
See moment.js - Display - Format for format specifiers.
Classic calendar formatting:
const daysBefore = moment().subtract(10, 'days').calendar();
for unix timestamp (current date minus 10 days):
const daysBefore = moment().subtract(10, 'days').unix();
Just remember, apply formatting after subtraction, as formatting returns a string, not momentjs object.
You can use moment().toDate() to get a Javascript Date object. Then you can get the timestamp with getTime().
calendar does not have method getTime(). You can format your time as follows:
let format = 'YYYYMMDD';
let a = moment().subtract(10, 'days').format(format);
console.log(a)
Try the following to get the timestamp:
moment().subtract(10, 'days')._d
try this one
moment().subtract(10, 'days').calendar().format('HH:MM:SS'); //get datetime
moment().subtract(1, 'days').calendar(); // Yesterday at 1:16 PM