Convert date format comes from database using jquery/javascript - javascript

I need to change my date format comes from database,for that I have seen many solutions but I need certain solution. here given my date
2015-08-15 02:54:43
I need to change this date into Aug-8 02:54 AM.
Please, provide me the certain solution
Thank you

Yes you can format Date in SQL query also, but if there is situation where you have to format in jquery then you can use:
Moment
Its a plugin, to Parse, validate, manipulate, and display dates in
JavaScript.
Instance:
$(function(){
var divLocal = $('#divLocal');
var localTime = moment("2015-08-15 02:54:43").toDate();
localTime = moment(localTime).format('MMMM-DD h:mm:ss A');//August-15 2:54:43 AM
//localTime = moment(localTime).format('MMMM-MM h:mm: A');//output August-08 2:54:43 AM
divLocal.text(localTime);
});
DEMO

you need to use date.js here below I have given an example
first of all include following both js in your page
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
then
write following script bottom of the page
<script>
var date = '2015-08-15 02:54:43' //your date
var parseDate = Date.parse(date);
alert(parseDate.toString("MMM-d hh:mm tt"));
</script>

Related

How to set a date string to a the following format

I'm using javascript and trying to convert a string to a different format. I did some research on this format but no one has asked about it yet so I thought I may ask. So I have seen that others want to convert a date string that has a shorter length. But this format is different. The format I have now by doing this:
const now = new Date();
const currentDate = now.toISOString();
I get this number:
2021-12-05T07:52:47.485Z
However, I want to make it the format like this:
2021-12-05T00:00:00.000+00:00
Is there any way to do so? I don't see others asking about this so not sure if possible
Use moment library:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Like this:
var now = new Date();
now.setHours(0,0,0,0);
var format = 'YYYY-MM-DD[T]HH:mm:ss.SSS[+00:00]';
console.log(moment(now).format(format));
Link: https://codepen.io/sdssz1365/pen/rNGORKR

How to get previous month and expected format from month picker using moment JS

I am using jquery.ui.monthpicker library. For month picker I am getting date like 07/2017. From this date string I need to calculate previous month and formatted like 1707 using moment js library.
any help would be appreciated.
This code may solve your problem.
moment("07/2017", "MM/YYYY").subtract(1, 'months').format('YYMM');
DEMO at https://jsfiddle.net/nffswx75/
var dt = "07/2017";
alert(moment(dt,"MM/YYYYY").format('YYMM'));
alert(moment(dt,"MM/YYYYY").add(-1, 'months').format('YYMM'));
alert(moment(dt,"MM/YYYYY").subtract(1, 'months').format('YYMM'));
You can let moment create a date object from a string by telling it what format your date is in.
let dateString: string = "07/2017";
var date = moment(dateString, "MM/YYYY").subtract(1, 'month').format("YYMM");

Formating this date type with moment.js

I have a javascript datetime picker which generated a time in this format: 2017-09-03T11:37:00.000Z
Now, I am trying to set default value of december 31, 2007 but I don't know how to generate it with the format the datepicker uses.
I have done jsfiddle:
var original = '2017-09-03T11:37:00.000Z';
var recreate = moment().endOf('year').format('YYYY-MM-HH:MM-SS:MS');
alert(recreate) // 2017-12-23:12-99:129
but the format isn't the same
This format is known as ISO date format. You can convert moment to this format by calling corresponding function:
var original = '2017-09-03T11:37:00.000Z';
var recreate = moment().endOf('year').toISOString();
alert(recreate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
See updated fiddle
toDate() to get it in correct format.
check this
var original = '2017-09-03T11:37:00.000Z';
var recreate = moment().endOf('year');
alert(moment(recreate).toDate());

Custom Javascript Variable in GTM to convert date (string) to date [duplicate]

This question already has answers here:
How to parse a date in format "YYYYmmdd" in JavaScript?
(6 answers)
Closed 6 years ago.
I am pulling a date of birth as a string from a form on a website im looking after. I need to be able to derive an age from that date string.
My thoughts are converting it into a proper date using a split (its delimited by "%2F") and calculating from that but my syntax isnt that good so im having real trouble.
The code im working with to pull the string I need is;
function() {
var inputField = document.getElementById("date-of-birth-input");
return inputField.value || "";
}
Any help would be appreciated.
If you have the user input the date in a format like 1/1/1990, you could do something like this:
var inputField = document.getElementById("date-of-birth-input"); // `1/1/1990`
var today = new Date().getFullYear();
var birthdate = new Date(inputField).getFullYear();
var age = today - birthdate;
The JavaScript Date constructor is pretty flexible in what it can turn into a date, so you might not need to go through splitting-string hassles. Check out the docs.
Date format varies in different countries. In US the date format is MM-DD-YYYY while in Asian countries the date format is DD-MM-YYYY and in China its 'YYYY-MM-DD'. The right way to go about dealing with dates is using Moment.js
If you have a simple application/website include moment in your <head> tag as follows
<script src="moment.js"></script>
(Hoping you have downloaded moment.js and kept in the same structure as index.html). Otherwise use this if you want to use it directly from the net without downloading.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/locale/af.js"></script>
Then use the following
function() {
/* First-make sure you format moment according to your giving locale.
I'm formating here according to the US date format */
var inputField = moment(document.getElementById("date-of-birth-input")).format("MM-DD-YYYY");
return moment().diff(inputField, 'years');
}
I hope this helps. Thanks.

Date formatting through Javascript - JQuery

Dates are always a kick in the nuts (at least for me) and I faced the fact that Javascript doesn't seem to have a method to format dates.
I'm trying to format a date to use the Google API which ask you for a date in this format: yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffff (e.g. 2011-06-25T10:00:00.000+02:00)
I need to be able to read that kind of string and to produce one.
I often use jQuery plugin: http://plugins.jquery.com/project/jquery-dateFormat
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="https://raw.github.com/phstc/jquery-dateFormat/master/jquery.dateFormat-1.0.js"></script>
<script type="text/javascript">
$(function() {
var d = new Date();
alert($.format.date(d, 'yyyy-MM-ddTHH:mm:ss'));
})
</script>

Categories

Resources