I have a string like this:
21.03.2016 23:59
And I need this string converted into a ISO-8601 date-time string:
YYYY-MM-DDTHH:mm:ss+00:00
Is there a simple way to convert this date?
I try it whit moment.js but i can't find a function to parse an existing date.
You can also do this without using moment.js.
Look code as following:
(new Date("03.21.2016 23:59")).toISOString()
just you need to change your string 21.03.2016 23:59 (dd-mm-yyyy) to 03.21.2016 23:59 (mm-dd-yyyy). You can easily do this by split the date and change the order of split part.
And if you dont want to do this then simply use moment.js as per matthias's answer.
Using moment.js you could do:
var dateString = '21.03.2016 23:59';
var momentDate = moment(dateString, 'DD.MM.YYYY HH:mm');
console.log(momentDate.toISOString());
Here is a fiddle showing this.
Related
I'm not so sure what's the proper way to insert date into the database, but I'm using new Date().
So I get date format like this when I query from the database:
2021-09-24T12:38:54.656Z
Now I realized that date format is not so user-friendly. So I'm trying to convert it if possible standard readable format like this:
Sept 25 2015, 8:00 PM
I tried using toLocaleString() to my date pulled from db but it won't work probably if I got it correctly pulled date from the db is already a string?
Is there a workaround for this so that I don't need to change how I enter date to my db?
const date = moment("2021-09-24T12:38:54.656Z").format('MMM D YYYY, h:mm a')
console.log(date)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
use momentjs
You can find format method and apply as you desire
https://momentjs.com/
I think it is better to store in DB as UTC 00 value and you are doing it right now.
When you retrieve it, you will be getting a string value something like, 2021-09-24T12:38:54.656Z. On the UI you can easily convert it to date variable in JS using,
const dateVar = new Date("2021-09-24T12:38:54.656Z");
console.log(dateVar.toLocaleString());
If you want more date formatting other than the inbuild solutions like toLocaleString you can use date libraries like moment.js
You'd need to create a Date object from the timestamp string like this.
let date = new Date('2021-09-24T12:38:54.656Z');
Then you can use toLocaleString, toLocaleDateString, toDateString, or toString as you see fit.
I'm trying to convert below-mentioned date to ISO format(MongoDB)
var d = { storedDate: '26/06/2020 05:55:29 PM' };
I'm however unable to find the parameter that I need to use to get it in the format which I want. I tried the below piece of code.
moment(d.storedDate).format("YYYY-MM-DD HH:mm Z");
How can I get it as ISODate("2020-06-26T17:55:29.274Z")
Please advice
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toISOString() will return ISO date only in UTC that you need for MongoDb. You need to provide the input date format as well.
If you want to store proper Date object, use
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toDate()
You should not store date/time values as strings, use proper data type.
How could I convert the string "2015-02-02" to ISODate 2015-02-02T00:00:00.000Z? I was trying to find some example but did not.
You can use the regular Javascript date functionality for this
new Date(dateString).toISOString()
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
However, date parsing is very inconsistent across browsers so if you need this to be robust I would look into parsing using for example with Moment.js as this will allow you to specify a format string by which the date should be parsed like such
date = moment("12-25-1995", "YYYY-MM-DD");
date.format(); //will return an ISO representation of the date
from: http://momentjs.com/docs/#/parsing/string/
To change "2015-02-02" to "2015-02-02T00:00:00.000Z" simply append "T00:00:00.000Z":
console.log('2015-02-02' + 'T00:00:00.000Z');
Parsing to a Date and calling toISOString will fail in browsers that don't correctly parse ISO dates and those that don't have toISOString.
new Date("2015-02-02").toISOString()
new Date("11/11/2019").toISOString()
or use it as a variable
mydate = "11/11/2019"
new Date(mydate).toISOString()
I have two inputs, a time and a date input. I'm trying to format them as an ISO string to send to the backend using moment.js.
This is what I have so far 01:00 2016-01-01, I need to format or convert that to ISO. Is there a way to do it using Moment?
To convert ISO I recommend the more standard
date.format();
or
JSON.stringify(yourDate)
or if you prefer momentjs:
var date = moment();
date.toISOString();
or
moment(yourDate).format('MM/DD/YYYY'); // <- your custom format string
To know what are the momentjs formatting rules start reading here
Assuming you are referring to ISO8601 and momentjs (2.10.6), I currently do it like this
var example = momentObject.format("YYYY-MM-DD[T]HH:mm:ss");
You need to use moment's parse function to first create the correct moment object from the data that you have (assuming a 24-hour clock, and month listed before the days):
var myMoment = moment("01:00 2016-01-01", "HH:mm YYYY-MM-DD");
Then you can use moment's format function to output the date in the ISO format that you want. Note that calling the format function without any parameters will output ISO 8601 by default:
myMoment.format();
See the moment docs for more info here.
Hope this helps!
I have a date like this:
2014-04-23T19:45:39 which is a UTC format.
I want to convert it to AST format or the localize time zone of the user. How to do it?
I suggest you use moment.js library and just add or subtract number of hours that AST time have comparing to UTC.
new_date = date.add('hours',4);
or
new_date = date.subtract('hours',4);
using timezone-js. you can easily convert the time from one timeZone to another. timezone.js
var date_object;
function localize(t){
date_object=new Date(t+" UTC");
document.write(date_object.toString());
}
localize("4/24/2014 4:52:48 PM")
document.write(date_object.toString().replace(/GMT.*/g,""));
Demo