This question already has answers here:
moment.js - UTC gives wrong date
(3 answers)
Closed 3 years ago.
I have a GMT date and time format coming from my dynamodb which I'm trying to convert to EST format using momentjs.
2019-06-27 20:00:43.156257
As soon as I drop the date into moment, it's converting it to +4 hours (when its supposed to be -4).
2019-06-28T00:00:43.156Z
All I'm doing is this.
const dbdate = [value-from-db]
const momentdate = moment(dbdate);
My output looks like:
dbdate: 2019-06-27 20:00:43.156257
momentdate: 2019-06-28T00:00:43.156Z
There are two issues here:
1) Moment is performing timezone conversion using your local timezone - use moment.utc instead
2) Your date is not in a format that moment "officially" supports - although actually it's relaxed enough to parse your string. Ideally, it should be provided in proper ISO 8601 format to avoid any compatibility issues.
You could try something like:
const dbdate = '2019-06-27 20:00:43.156257'.split(' ');
const momentdate = moment.utc(dbdate[0] + 'T' + dbdate[1] + 'Z');
alert(momentdate);
Here's a fiddle.
Hope this helps!
You must use moment.utc() instead of moment():
const dbdate = '2019-06-27 20:00:43.156257';
const momentdate = moment(dbdate);
const utcmomentdate = moment.utc(dbdate);
console.log('local: \n', momentdate);
console.log('utc: \n', utcmomentdate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Related
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 5 months ago.
When working with the task, it became necessary to get dates from html, and to find out the time difference between them:
var now_time_div = document.getElementById("time_now_of");
var start_time_div = document.getElementById("time_when_start_of");
var time_now = now_time_div.textContent || now_time_div.innerHTML;
var time_start = start_time_div.textContent || start_time_div.innerHTML;
After that, without thinking about the format of the data, I wanted to find the time difference in ms:
var worked_time = time_now - time_start
That didn't work, because we are working with a string.
After entering the directory, I found the Date.parse() function, which returns the amount of time that has passed since the given date:
var worked_time = Date.parse(time_start);
but it turned out that it works only with a correctly submitted strig, for example
We need to have:
Date.parse('01 Jan 1970 00:00:00 GMT');
We have:
Date.parse('21.09.2022, 15:34:21')
Maybe someone knows an easy way to implement this without rebuilding the string?
If you don't want to bring in a library like moment.js, you can just massage the date string a bit so it can be parsed correctly
const dateString = '21.09.2022, 15:34:21';
const curDate = dateString.split(',')[0].substring(0, 10).split('.');
const curTime = dateString.split(',')[1];
const parsed = `${curDate[1]}'/'${curDate[0]}'/'${curDate[2]} ${curTime}`;
console.log(new Date(parsed).toString()); // Properly formatted date
You can used this parsed variable to compare to other properly formatted dates
You can use moment.js to parse custom date formats just as in for example Java:
https://momentjs.com/docs/#/parsing/string-format/
After that you can simply convert it to a js date using the toDate function: https://momentjs.com/docs/#/displaying/as-javascript-date/
Edit Example:
var mDate = moment('2022-09-21 10:15:00', 'YYYY-MM-DD HH:mm:ss');
var jsDate = mDate.toDate();
This question already has answers here:
Format a date string in javascript
(7 answers)
Closed 2 years ago.
How do I format the date I receive from openweather api?
I currently get 2020-10-16 00:00:00 and I want 10-16-2020.
The reason I don't use moment is because I want future dates which come automatically with the 5 day forecast in the api.
You can use JavaScript's Date object.
You might save yourself time by searching a bit more before posting a question, the answer is probably already out there.
Where can I find documentation on formatting a date in JavaScript?
How to format a JavaScript date
Format JavaScript date as yyyy-mm-dd
You could try to:
Make a new date based on the date that comes from OpenWeather api
Convert it to a LocaleDateString using the 'en-US' locale. This will make the month appear before the date.
Then just split the Date String on the '/' and join in on a '-'. This will substitute the '/' with a '-'
const date = new Date("2020-10-16 00:00:00").toLocaleDateString('en-US');
const formatedDate = date.split('/').join('-');
console.log(formatedDate);
You can always use the built in Javascript Date object.
In your case you'd want to. do something like this -
const myDate = new Date('2020-10-16 00:00:00');
const date = myDate.getDate();
const month = myDate.getMonth() + 1;
const year = myDate.getFullYear();
console.log('MM-DD-YYYY', `${month}-${date}-${year}`);
If you want something more convinient and don't want to use moment you can also try some other popular date libraries. You can find some here - https://github.com/you-dont-need/You-Dont-Need-Momentjs
This question already has answers here:
Format JavaScript date as yyyy-mm-dd
(50 answers)
Closed 2 years ago.
I have date string in the format '2020/02/25 23:58:08' . I want to parse it to '2020-02-25".
Note : Initially date is in string format, after conversion whether it is in date or string format it doesn't matter.
file.js
function test (filepath) {
let date = "2020/02/25 23:58:08";
date = date.replace("//"/gi,"-");
// I am not familiar with regular expressions, I want to omit the data related to hours, seconds extra
}
When I ran the program, I gotUncaught ReferenceError: gi is not defined, gi is to globally replace
Try this
let date = "2020/02/25 23:58:08";
var date2 = new Date(date)
console.log(date2.toISOString().slice(0,10))
let date = `2020/02/25 23:58:08`;
let parsedDate = date.split(" ")[0].replace(/\//gi,'-');
console.log(parsedDate);
There is an error in the line using regex, should be:
let date = "2020/02/25 23:58:08";
date = date.replace(/\//gi,"-");
Then to get only the date, you can get the first 10 characters:
date.slice(0,10)
Final code:
let date = "2020/02/25 23:58:08";
date = date.replace(/\//gi,"-");
date.slice(0,10)
Although there are other ways to do it, you can use a library like momentJs which includes methods for this, like moment.format, it gives so many possibilities. But if you have a small case this is fine.
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I have this date here 2013/06/10, which comes from the database and is set in a variable called date.
I added one day to this date by doing this..
var endDate = new Date(date);
endDate.setDate(endDate.getDate() + 1);
and now I am trying to change the format to yyyy/MM/dd
var finalEndDate = endDate.toString('yyyy/MM/dd');
alert(finalEndDate);
but this returns
Tues Jun 11 2013 Eastern Standard Time, etc.
How do I fix this?
As far as I know, toString does not take any arguments. It's easy to construct your format though.
var finalEndDate = endDate.getFullYear() + '/' + (endDate.getMonth() + 1) + '/' + endDate.getDate();
There are several getter methods for each component of the date object to help you construct nearly any format.
I strongly encourage you to take a look at Moment.js
var str = moment(date, 'YYYY/MM/DD').add('days', 1).format('yyyy/MM/dd');
Note: moment doesn't know yyyy, what's that supposed to be? See http://momentjs.com/docs/#/displaying/format/ for supported format strings.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I output an ISO-8601 formatted string in Javascript?
I am trying to convert a date and time input to an ISO format but I am getting .toISOString is undefined? I have to be missing something silly.
var startDate = "10/11/2012";
var startTime = "12:12:00";
var fullDate = startDate + " " + startTime;
var fullDateReal = new Date(fullDate);
var iso = fullDateReal.toISOString();
Why would .toISOString() show as undefined?
I need to end up with the ISO format ("2012-10-11T12:12") timezone is optional.
Update
It looks like this problem is because IE8 does not support this. So how could I go about converting my inputs to the format listed?
Some browsers don't support ECMAScript 5 (which is required for toISOString).
http://kangax.github.com/es5-compat-table/