I have date in this format:
2022-06-28T17:09:00.922108+01:00
I want to covert it into a more readable format.
I'm using javascript/react and I tried moment-js but it gives me "invalid format".
What are my other options of turning it into a normal date?
You can use this to format date:
const format = "DD-MM-YYYY"
moment(date).format(format)
or
moment().format('MMMM Do YYYY, h:mm:ss a'); // June 28th 2022, 9:49:49 pm
for more detail refer: https://momentjs.com/
If you are using moment.js you can convert this to any format you like using this doc
Sample code
moment('2022-06-28T17:09:00.922108+01:00').format("DD-MM-YYYY"); //28-06-2022
Kindly go though the moment.js features and documents. There is a lot more you can do with this like showing time, day of the week etc
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.
Want to remove TimeZone details from date+time.(EmberJS)
Input: "2019-03-11T09:00:00.000+09:00" (GMT+9) .
Like to convert in this format "2019-03-11T09:00:00.000+00:00" (GMT+0)
Usecase:
API Returning : timestamp: "2019-03-11T09:00:00.000+09:00"
UI TimeZone is : UTC+9
Currently UI Display: 3/11/2019 18:00
UI should show 3/11/2019 9:00
Try using ember-moment addon
{{moment-format '12-1995-25' 'MM/DD/YYYY' 'MM-YYYY-DD'}}
Replace 12-1995-25 with your API returning date
If you just want to drop timezone from date that you receive as a string, you just need to use regular expression to remove '+09:00' part:
new Date('2019-03-11T09:00:00.000+09:00'.replace(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})[+\-]\d{2}:\d{2}/, '$1'))
Or, to replace timezone with UTC:
new Date('2019-03-11T09:00:00.000+09:00'.replace(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})[+\-]\d{2}:\d{2}/, '$1+00:00'))
If you want to convert between timezones you can use moment-timezone or luxon
I'm using angularjs and mongodb.
I have not found a angularjs date slider so I'm taking this one. The idea is to use a timestamp with a step a day. So I take this timestamp convert it to a dd mmmm yyyy format and be displayed in the front-end.
Assuming as I said I use mongodb what can be a good solution for the date number format?
If you are looking for a date time library, try moment.js
It is very simple to use. Include the script file using <script> tag. Then use it like this.
moment().format('MMMM Do YYYY, h:mm:ss a');
This will return November 6th 2013, 1:35:00 pm
If you need to show the time in human format use fromNow() like this moment("20111031", "YYYYMMDD").fromNow(); This will result 2 years ago .
I would like to be able to convert a Java date format string, e.g. dd/MM/yyyy (07/06/2009) to a JavaScript date format string, e.g. dd/mm/yy (07/06/2009).
Has anyone done this before, or got any idea where I might find some code that already does this?
Edit:
Thanks for all the replies but now I realize my mistake and possibly why so many of you were struggling to understand the question; JavaScript doesn't have a built in date formatting ability. I am using the jQuery UI datepicker and I have been setting its date format, assuming it would be calling a standard JS function at some point, not using its own library! When I googled for formatting strings I jumped straight to the tables of what letters could be used, skipping the bit at the beginning explaining how to use the script.
Anyway I'll have to go ahead and possibly write my own I guess, converting a Java date format string into a jQuery date format string (or as close as possible) - I am working on the i18n of our product and have created a java class that stores the preferred date format string used throughout the application, my intention was to also have the ability to supply any jsps with the format string that is equivalent in JS.
Thanks anyway.
If you just need to pass a date from Java to JavaScript, the best way to do it, I think, would be to convert the Java date to milliseconds using date.getTime(), create a JavaScript date initialized with this milliseconds value with new Date(milliseconds)and then format the date with the means of the JavaScript Date object, like: date.toLocaleString().
You could use my plugin jquery-dateFormat.
// Text
$.format.date("2009-12-18 10:54:50.546", "dd/MM/yyyy");
// HTML Object
$.format.date($("#spanDate").text(), "dd/MM/yyyy");
// Scriptlet
$.format.date("<%=java.util.Date().toString()%>", "dd/MM/yyyy");
// JSON
var obj = ajaxRequest();
$.format.date(obj.date, "dd/MM/yyyy");
A similar topic has been answered here:
Converting dates in JavaScript
I personally have found this to be a rather large pain and took the author's suggestion and used a library. As noted, jQuery datepicker has one that is a viable solution if you can afford the overhead of download for your application or already using it.
Check out moment.js! It's "A lightweight javascript date library for parsing, manipulating, and formatting dates". It is a really powerful little library.
Here's an example...
var today = moment(new Date());
today.format("MMMM D, YYYY h:m A"); // outputs "April 11, 2012 2:32 PM"
// in one line...
moment().format("MMMM D, YYYY h:m A"); // outputs "April 11, 2012 2:32 PM"
Here's another example...
var a = moment([2012, 2, 12, 15, 25, 50, 125]);
a.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Monday, March 12th 2012, 3:25:50 pm"
a.format("ddd, hA"); // "Mon, 3PM"
a.format("D/M/YYYY"); // "12/3/2012"
Also, its worth mentioning to checkout date.js. I think the two libraries complement each other.
This JavaScript library should be able to help you.
http://plugins.jquery.com/project/fIsForFormat
(I don't know why they have it as a jQuery Plugin, because it works standalone.)
You'd simply split the original formatted date into its individual elements and then create a new Date Object with those elements. Then, use this library's "Date.f()" method to output it into any format you could want.
For example:
var dateOld = "11/27/2010",
dateArr = date1.split("/"),
dateObj = new Date(dateArr[2], dateArr[0], dateArr[1]),
dateNew = dateObj.f("MMM d, yyyy");
document.write("Old Format: " + dateOld + "<br/>New Format: " + dateNew);
This works fine for me:
<%
Date date = Calendar.getInstance().getTime();
%>
<script>
var d = new Date(<%=date.getTime()%>);
alert(d);
</script>
I suggest you the MomentJS with this Plugin that allow you to convert a Java pattern to a JS pattern (MomentJS)
On Java Side
I recommend passing an Instant string which conforms to ISO 8601 standard.
import java.time.Instant;
class Main {
public static void main(String[] args) {
Instant instant = Instant.now();
// You can pass the following string to JavaScript
String strInstant = instant.toString();
System.out.println(strInstant);
// If the number of milliseconds from epoch is required
long millis = instant.toEpochMilli();
System.out.println(millis);
}
}
Output from a sample run:
2022-12-31T09:40:52.280726Z
1672479652280
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
On JavaScript Side
Now, you can parse the ISO 8601 string on the JavaScript side simply by passing it as a parameter to Date constructor. You can also instantiate the Date object with the number of milliseconds from the epoch.
var date = new Date("2022-12-31T09:40:52.280726Z");
console.log(date.toISOString());
// Or if the number of milliseconds from epoch has been received
date = new Date(1672479652280);
console.log(date.toISOString());
The javascript code in this page implements some date functions and they "use the same format strings as the java.text.SimpleDateFormat class, with a few minor exceptions". It is not the very same as you want but it can be a good start point.
If you just want to format dates my date extensions will do that well - it also parses data formats and does a lot of date math/compares as well:
DP_DateExtensions Library
Not sure if it'll help, but I've found it invaluable in several projects.
If you are using java, take a look at the Simple Date Format class.