In my Rails application, in JSON responses from controller, dates are coming as 2015-06-22T18:08:22+05:30. I want to convert the date into 22-06-2015, 06:08 PM format in Javascript. I checked may resources in Stackoverflow but none is working. Is there any way to do that?
you can use date.format library
Here's a example fiddle
var date = new Date("2015-06-22T18:08:22+05:30");
var dateString = date.format("dd-mm-yyyy hh:MM:ss TT Z");
alert(dateString);
Also it would be better to format it on server side if you dont want to include extra libraries
moment is a very good library for dealing with date.
console.log(moment('2015-06-22T18:08:22+05:30').format('DD-MM-YYYY h:mm:ss a'));
> 22-06-2015 7:38:22 pm
There are Two Ways:
Do the conversion in the Rails and send the value via JSON (using strftime or u can use Date.strptime or Time.strptime)
Code:
User.created_at.strftime("%d/%m/%Y")
Doing the conversion in Front End using Moment Js:
Code:
console.log(moment('2015-06-22T18:08:22+05:30').format('DD-MM-YYYY h:mm:ss a'));
Related
I am getting date data in this format from my API:
"2018-12-26T05:00:29"
however I need to display this in the application front end in a different format like this:
"Monday, Nov 26 at 10:00 am"
How can I achieve this in react-native?
Consider using a third-party library like momentjs for advanced date parsing and formatting. Using moment, you can format the date string as required via the following pattern:
// dddd for full week day, MMM for abreviated month, DD for date, etc
moment(inputDate).format("dddd, MMM DD at HH:mm a")
The momentjs library works well with react-native and can be easily installed by:
npm install moment --save
and imported into your project:
import moment from 'moment';
Here's a snippet demonstrating the pattern shown above:
var inputDate = "2018-12-26T05:00:29";
var outputDate = moment(inputDate).format("dddd, MMM DD at HH:mm a");
console.log(outputDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
While Moment.js can resolve this issue quickly, it's however a pretty big library to use for a single use case. I recommend using date-fns instead. It's lite.
I come out of this thanks to https://github.com/date-fns/date-fns/issues/376#issuecomment-353871093.
Despite the solution, I did some perfection on Date Field Symbol. You may need to use dd instead of DD / yyyy instead of YYYY for formatting days of the month / for formatting years.
Read the doc here: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
I have to store the date in the database using this format YY-MM-DD hh:mm:ss but I get this 12/20/2018 1:30 AM after select date from the kendo-datetime picker.
I have tried fomat="YY-MM-SS hh:mm:ss" but its not working every time getting above the same format.
Tell me, anyone, how to convert selected date and time to YY-MM-DD hh:mm:ss?
var date = object.dTime;
var newdate = date.split("/").reverse().join("-");
Use moment library in order to format the date into whatever way that you want to display,
moment(date).format('YY-MM-SS hh:mm:ss')
For more detail, look at here
You can use in angular moment js please read moment js implementation.
or you can follow this link both question are something same .
[format date with moment.js
In your html file use this
<input kendo-date-picker
k-format="'dd/MM/yyyy'"
ng-model="model.Date"
name="Date" required />
var date = new Date('12/20/2018 1:30 AM')
date.toISOString().replace('T', ' ').substring(2, 11) + date.toTimeString().substring(0, 8) // "18-12-19 01:30:00"
i select calendar and set "1517380775843" console , i try convert and get format = 20180131093935 GMT+03:00 ,not now time i try select time and convert to human readable data pls help . thanks
<DateTimeField onChange={this.clock} format={"x"} inputFormat={"YYYY/DD/MM HH:mm:ss"}/>
clock=(newDate)=>{
console.log(newDate)
}
Use moment JS to do such operations.
For example:
moment.parseZone(element.requestDate).format('DD-MM-YYYY hh:mm A')
or for converting to a format specified:
moment(dateString,'DD-MM-YYYY hh:mm A')
Visit https://momentjs.com/ for the docs
Basically I have date format which comes from server and which will be not known before and a date that was already formatted in this format, e.g. the format is "dd/MM/yyyy" and the date is "21/01/2018" (two strings). The format that uses jqx differs from the default JS one, as I can see.
What I need to do is to parse it to JavaScript date object. I couldn't find the answer in the official documentation.
Is there any easy was of parsing the date string if you know the date format, using JS, jqwidgets or momentjs?
Eventually, I've finished with inner jqx method that does that exact thing:
var parsedDate = $.jqx.dataFormat.parsedate(src, format);
Where format in this case is "dd/MM/yyyy" and src is "21/01/2018".
P.S. Thanks for the guy that posted anwer with the code that used $.jqx.dataFormat, that really helped me to find parsedate method.
I have a time as a string like "10:00pm" and I need to convert it into a date object so it can be represented in an input type="time" tag. Im using angularjs to model it.
I get an error saying "Expected 10:00am to be a date". How can I convert "10:00am" into a date object? I haven't been able to figure it out. Any help is appreciated.
I only need the time
Have you seen http://momentjs.com/ ?
You can use it like this:
var time = moment("10:00pm", "HH:mm a");
If you're using date.js then you could try
Date.parseExact("10:00 PM", "hh:mm tt");
This should also pick up if you've loaded the library correctly.