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/
Related
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 1 year ago.
Let's say I have a string 2021-08-13 and want to convert this to August 13, 2021. How would you achieve this as it's not a date object.
In my mind I can think of setting each numeric month to a text version of that month and re-arrange, however seeing if there are better ways of doing this.
Simple: convert the string into a Date object and use the toLocaleString function.
If you want to get rid of the timezone so the date stays the same wherever the user is you can first convert it into an ISO string, get rid of the 'Z' in the end, and then convert it back into the Date object.
const dateString = '2021-08-13'
const localeOptions = {dateStyle: 'long'}
const dateTimezone = new Date(dateString).toLocaleString('en-US', localeOptions)
const dateWithoutTimezone = new Date(new Date(dateString).toISOString().slice(0,-1)).toLocaleString('en-US', localeOptions)
console.log(dateTimezone)
console.log(dateWithoutTimezone)
Convert your string date to a JS Date Object
let strDate = "2021-08-13";
let date = new Date(strDate);
console.log(date.toDateString())
Learn more about Date object here: JavaScript Date
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:
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>
This question already has answers here:
javascript: how to parse a date string
(4 answers)
Closed 5 years ago.
In javascript, while using exif-js to extract metadata of an image file, I am getting date time format as 2017:03:09 14:49:21.
The value in the DateTimeOriginal property is formatted as YYYY:MMY:DD HH:MM:SS. When I use var d = new Date(2017:03:09 14:49:21), it returns NaN. It's the colons in between the YYYY, MM, and DD which causes problem.
How to solve this problem?
Thanks in advance.
Don't use the built-in parser (i.e. Date constructor or Date.parse) for parsing strings as it's largely implementation dependent and unreliable. If you can trust the date to be valid, then the following will do:
/* Parse date string in YYYY-MM-DD hh:mm:ss format
** separator can be any non-digit character
** e.g. 2017:03:09 14:49:21
*/
function parseDate(s) {
var b = s.split(/\D/);
return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5]);
}
console.log(parseDate('2017:03:09 14:49:21').toString());
It's fairly easy to add validation to the values. Otherwise, use a library and make sure you specify the format to parse.
My recommendation would be to use Moment (http://momentjs.com/docs/), as it provides clean parsing of dates. With Moment, what you want is this:
var tstamp = moment("2017:03:09 14:49:21", "YYYY:MM:DD HH:mm:ss");
var date = tstamp.toDate();
You can do simple string manipulation and create date if the format is always the same, as:
var str = "2017:03:09 14:49:21".split(" ");
//get date part and replace ':' with '-'
var dateStr = str[0].replace(/:/g, "-");
//concat the strings (date and time part)
var properDateStr = dateStr + " " + str[1];
//pass to Date
var date = new Date(properDateStr);
console.log(date);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert UTC Epoch to local date with javascript
In my project,
I am receiving a JSON. it contains one field like
"displayDate":"1349137055814",
How can I convert to user readable (understandable, to be specific ;) ) form?
For eg mm-dd-yyyy
And what is this format?
new Date(milliseconds) creates a new date object using your millseconds value.
You can then use getYear, getDate, toLocaleFormat() or toUTCString to get your correct format.
You can convert it to a date using the Javascript Date object.
// Assuming 'result' contains your JSON
var myDate = new Date(result.displayDate);
Use it this way
var d = new Date(1349137055814);
alert(d.getMonth()+ '/' + d.getDate() + '/' + d.getFullYear());
Check this demo